Logging into form with cURL/PHP

217 views Asked by At

I'm trying to login to an external site with PHP's curl protocol.

This is the form in question

<form accept-charset="UTF-8" action="/support/login" method="post">
    <input name="utf8" type="hidden" value="&#x2713;" />
    <input name="authenticity_token" type="hidden" value="pHEFLN4N1cLB/L8hnL1BBD6MK6tZ1s5VFceXlLaM48o="/>
    <input class="required special" id="user_session_email" name="user_session[email]" type="email" value="" />
    <input class="special" id="user_session_password" name="user_session[password]" type="password">
    <input name="user_session[remember_me]" type="hidden" value="0" />
    <input checked="checked" id="user_session_remember_me" name="user_session[remember_me]" type="checkbox" value="1" />
    <button type="submit" class="btn btn-primary btn-login"> Login</button>
</form>

And this is what I have so far:

$ch = curl_init();
$url = "example_url";
$username = "[email protected]";
$password = "example_password";
$postInfo = http_build_query(array('user_session[email]' => $username, 'user_session[password]' => $password));

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_REFERER, $_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postInfo);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

I know that I need to build in handling for the authenticity token, as well as user session storage (cookie jar?) and possibly the "remember me" button too. But, right now I'm getting an error from the return html that no verification information was registered at all, so clearly nothing I have is working.

0

There are 0 answers