How to Log In with Curl Using SSL and Cookies
Problem:
Logging into a website using Curl, specifically barnesandnoble.com, is not successful. The page is returned without errors, but the email field is filled with the initial email entered. Changing the login URL to a non-HTTPS website (i.e., eBay) allows successful login.
Cause:
Potential causes include:
Solution:
The following code addresses these issues and demonstrates successful login using Curl, SSL, and cookies:
</p> <p>//...<br>// Previous curl options omitted for brevity<br>//...</p> <p>// Get form fields<br>$fields = getFormFields($content);</p> <p>// Set email and password<br>$fields['emailAddress'] = $EMAIL;<br>$fields['acctPassword'] = $PASSWORD;</p> <p>// Get x value<br>if (preg_match('/op.asp?x=(d )/i', $content, $match)) {</p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">$x = $match[1];
}
// Set login URL with x value
$LOGINURL = "https://cart2.barnesandnoble.com/mobileacct/op.asp?x=$x";
// Set POST fields
$POSTFIELDS = http_build_query($fields);
// Change URL to login URL
curl_setopt($ch, CURLOPT_URL, $LOGINURL);
// Set post options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
// Perform login
$result = curl_exec($ch);
print $result;
//...
Explanation:
By implementing these changes, Curl can successfully log in despite the use of HTTPS and the potential differences in cookie handling.
The above is the detailed content of How to Log In to Websites with SSL and Cookies Using Curl?. For more information, please follow other related articles on the PHP Chinese website!