login into webpage with php with cURL
<p>I bought a book on web scraping with php. In it the author logins into <a href="https://www.packtpub.com/" rel="nofollow noreferrer">https://www.packtpub.com/</a> . 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 <code>$results</code> variable. What should I change? I believe the error is coming from incorrectly specifying destination.</p>

<pre><code>&lt;?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 = 'youremail@email.com';  // 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' =&gt; 'value'
$postFields = array(
        'email' =&gt; $userEmail,
        'password' =&gt; $userPass,
        'destination' =&gt; 'https://www.packtpub.com',
        'form_id' =&gt; 'packt-user-login-form'
);
$successString = 'Account Options';
$loggedIn = curlPost($postUrl, $postFields, $successString);  //Executing curlPost login and storing results page in $loggedIn
</code></pre>

<p>EDIT: post request:</p>

<p><a href="https://i.stack.imgur.com/mBCva.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mBCva.png" alt="enter image description here"></a></p>

<p>I replaced the line             </p>

<pre><code>'destination' =&gt; 'https://www.packtpub.com'
with    

'op' =&gt; 'Login'
</code></pre>

<p>,added </p>

<pre><code>'form_build_id' =&gt; ''
</code></pre>

<p>and edited</p>

<pre><code>$postUrl = 'https://www.packtpub.com/register';
</code></pre>

<p>since that is the URL I get when choosing copy as cURL and pasting in editor.</p>

<p>I am still getting "Nope, sth went wrong message". I think it is because <code>$successString</code> 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.</p>