使用PHP和CURL登录网站

时间:2017-01-10 02:19:14

标签: php curl

我在发布https://example.com/my-receipts登录信息后尝试抓取页面https://example.com/sessions。我已正确登录,但我没有被带到/my-receipts页面(我可以看到我已从结果页面登录,但它已经捕获了一些例外并将我重定向到帮助页面。)

这是我使用的代码:

//initial request with login data

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com/sessions');
curl_setopt($ch, CURLOPT_REFERER, "https://example.com/dashboard");
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'email='.urlencode($email).'&password='.urlencode($password).'&_token=');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, '4.cookie');
curl_setopt($ch, CURLOPT_COOKIEFILE, '4.cookie');
$answer = curl_exec($ch);
if (curl_error($ch)) {
    echo "[E1] ".curl_error($ch);
}

//another request preserving the session

curl_setopt($ch, CURLOPT_URL, 'https://example.com/my-receipts');
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
$answer = curl_exec($ch);
if (curl_error($ch)) {
    echo "[E2] ".curl_error($ch);
}

echo $answer;

我试图从中获取页面的网站是在nginx上使用Angular JS运行Laravel。使用此代码登录后,我能够获取其他页面。是否可能存在一些特定于框架的东西阻止了我?

0 个答案:

没有答案