简体   繁体   中英

cURL follow redirects for Login form

I'm trying to use PHP cURL to login to a website and go to a specific url to retrieve it's contents.

This is the page I'm trying to view: https://dev-portal.getpebble.com/developer

However this page requires the user to be logged in, the login page is on: https://auth.getpebble.com/users/sign_in

The odd part is that the login page, has a hidden value "authenticity_token" which must be submitted with the form. So my current process is:

  1. cURL to get content of login page. (calling cURL on auth url)
  2. Fetch "Authenticity token" from page (retrieve token for post data)
  3. cURL to submit to login page with authenticity token + credentials (calling cURL on Auth url with post data)

Which works as seen in this image (screenshot of curl content from exec): http://i.gyazo.com/4eccb78bf18f72f1da65bc9d8967541e.png

However this process brings me to: https://auth.getpebble.com/me and I now need cURL to bring me to https://dev-portal.getpebble.com/developer using the authenticated session I have. I've tried saving the cookies but it doesn't appear to work.

After step 3 above I've tried calling cURL on the https://dev-portal.getpebble.com/developer page but I just keep getting redirected to "localhost/users/auth/pebble" (which doesn't even exist?? I assume cURL is causing the page to redirect?) and if I cancel the redirect before it occurs I get the unauthenticated page: http://i.gyazo.com/218e00c262651017df4dcc1927def87f.png

Here is my code to create the cURL requests.

    private function cURL($url, $postData = false)
{

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_FORBID_REUSE, TRUE);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
    curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    if ($postData)
    {
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
    }

    $content = $this->content = curl_exec($ch);

    echo $content;

    curl_close($ch);
    return $content;
}

Turns out the cookie was being generated on a different page rather than https://dev-portal.getpebble.com/developer hence this page would create an invalid session.

I managed to fix it by using https://dev-portal.getpebble.com/users/auth/pebble instead of https://dev-portal.getpebble.com/developer and though following redirects it will bring me to the authenticated /developer page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM