I bought a book on web scraping with php. In it the author logins into https://www.packtpub.com/ . The book is out of date so I can't really test ideas out, because the webpage has changed since release. This is the modified code I am using, but the logins are unsuccessful, which I concluded from "Account Options" string not being in the $results variable. What should I change? I believe the error is coming from incorrectly specifying destination.
<?php
// Function to submit form using cURL POST method
function curlPost($postUrl, $postFields, $successString) {
$useragent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5;
en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3'; // Setting useragent of a popular browser
$cookie = 'cookie.txt'; // Setting a cookie file to storecookie
$ch = curl_init(); // Initialising cURL session
// Setting cURL options
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // PreventcURL from verifying SSL certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE); // Script shouldfail silently on error
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE); // Use cookies
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // FollowLocation: headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Returningtransfer as a string
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); // Settingcookiefile
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); // Settingcookiejar
curl_setopt($ch, CURLOPT_USERAGENT, $useragent); // Settinguseragent
curl_setopt($ch, CURLOPT_URL, $postUrl); // Setting URL to POSTto
curl_setopt($ch, CURLOPT_POST, TRUE); // Setting method as POST
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postFields)); // Setting POST fields as array
$results = curl_exec($ch); // Executing cURL session
$httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
echo "$httpcode";
curl_close($ch); // Closing cURL session
// Checking if login was successful by checking existence of string
if (strpos($results, $successString)) {
echo "I'm in.";
return $results;
} else {
echo "Nope, sth went wrong.";
return FALSE;
}
}
$userEmail = '[email protected]'; // Setting your email address for site login
$userPass = 'yourpass'; // Setting your password for sitelogin
$postUrl = 'https://www.packtpub.com'; // Setting URL toPOST to
// Setting form input fields as 'name' => 'value'
$postFields = array(
'email' => $userEmail,
'password' => $userPass,
'destination' => 'https://www.packtpub.com',
'form_id' => 'packt-user-login-form'
);
$successString = 'Account Options';
$loggedIn = curlPost($postUrl, $postFields, $successString); //Executing curlPost login and storing results page in $loggedIn
EDIT: post request:
I replaced the line
'destination' => 'https://www.packtpub.com'
with
'op' => 'Login'
,added
'form_build_id' => ''
and edited
$postUrl = 'https://www.packtpub.com/register';
since that is the URL I get when choosing copy as cURL and pasting in editor.
I am still getting "Nope, sth went wrong message". I think it is because $successString doesn't get stored in curl in the first place. What is the form-build-id supposed to be set to? It is changing every time I log in.

form_build_idmay be a CSRF token. If it is, you will have to make a request to the login page (GET request), then parse the HTML to extract this value. It's likely in a hidden form field. Try to replay the request in Firefox with a blankform_build_idand check the response.form_build_idis a CSRF token. They seem to be using Drupal. I don't have time right now to write the cURL request in PHP. If I have time when I return home I will knock up an example for you. Here's some useful information on what a CSRF token is, and why they are used: owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29-instead of_in theform_id:p