I need to load a url, type in a username/password and submit the form. Then go to another URL and parse the data. (The last URL is password protected, which is why I need to submit the form.
So I am able to load the first url find the input fill them out. I then need to submit the form and get the next url. Does anyone know how I can do that? I don't have much experience with the PHP dom library.
This is the code I have so far:
Code: Select all
$username = 'usernamegoeshere';
$password = 'passgoeshere';
$dom = new DOMDocument();
$dom->loadHTMLFile('https://urltologin.com');
$elements = $dom->getElementsByTagName('*');
if(!is_null($elements))
{
foreach ($elements as $element)
{
echo "<br/>". $element->nodeName. ": ";
if($element->nodeName == "input")
{
if($element->getAttribute('id') == "MerchantLogin")
{
echo "Found Login";
$login_input = $element;
}
else if($element->getAttribute('name') == "Password")
{
$password_input = $element;
echo "Found Password";
}
else if($element->getAttribute('value') == "Log In")
{
$submit_input = $element;
echo "Found Submit Button";
}
}
}
}
$login_input->nodeValue = $username;
$login_input->setAttribute("value", $username);
$password_input->setAttribute("value", $password);
$password_input->nodeValue = $password;
$submit_input->click();
$submit_input->submit();