0

I am using PHP to CURL login to a website. I am trying to automate the changing of my password.

I can login to the site and then am forwarded to the Change password page.

How do I post data to "Change Password" page when it has been returned in a CURL?

This is what I use to call the login page.

$ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_NOBODY, false);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);

    //set the cookie the site has for certain features, this is optional
    curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0");
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:44.0) Gecko/20100101 Firefox/44.0");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
    curl_exec($ch);

    //page with the content I want to grab
    curl_setopt($ch, CURLOPT_URL, "myurl.com");

    //do stuff with the info with DomDocument() etc
    $html = curl_exec($ch);
    echo $html;
    curl_close($ch);

It's HTML returns: enter image description here

It should be noted that the URL never changes, the login page simply submits it self and refreshes with the new boxes.

Can I use Javascript to fill out the form and submit it while it is in the $html variable??

user-44651
  • 3,924
  • 6
  • 41
  • 87
  • 1
    Are you really saying that you want Javascript to execute in a string variable within PHP? – Paul Carlton Mar 08 '16 at 21:06
  • I'm asking if it is possible to fill out the returned form and submit it. – user-44651 Mar 08 '16 at 21:07
  • lol, I was like "WUT? No. No. no no no no... Nope." Anyway, no you can't do that with cURL. You want a library that mimics a browser and will execute the Javascript so you can inject javascript. cURL does not do that, all it does is return the page content, so you are looking for something like CasperJS or PhantomJS which is in a completely different language. – Paul Carlton Mar 08 '16 at 21:16
  • I can't make a second request in the same context and post additional data? – user-44651 Mar 08 '16 at 21:40
  • Each post requires a new cURL request. Ensure you are using cookies with cURL, then abstract your cURL logic into a function and then make consecutive calls with the data you get back from the pages you are parsing. – Paul Carlton Mar 08 '16 at 22:36
  • See [this answer](http://stackoverflow.com/questions/33662299/login-with-curl-and-move-to-another-page/33716781#33716781) for numerous examples using cURL to login to websites and perform actions once logged in. You just need to look at the password change form and emulate it's POST request once you login, re-using the original cURL handle you used to log in. – drew010 Mar 09 '16 at 05:45

0 Answers0