使用cURL登录网站

时间:2015-10-28 23:40:50

标签: php curl httprequest

我有一个有效的cURL代码,我已经在我自己的网站上测试了它,它可以工作,所以现在我试图在其他网站上测试它,以确保它也能正常工作。我在stackoverflow上测试了它,但它返回了意外的结果。我做错了什么?

$ch = curl_init('https://stackoverflow.com/users/login?ssrc=head&returnurl=http%3a%2f%2fstackoverflow.com%2f');
$post_params = array('email' => 'frosty1234@mail.com', 'password' => 'password');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params); // PHP will format the array into POST data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);

echo $result;

结果:

Object moved to here.

^ ---点击此处,将我带到stackoverflow的主页面。但是,我没有登录。我做错了什么?

1 个答案:

答案 0 :(得分:0)

看起来它正在工作,你只需告诉curl遵循重定向:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

可能

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);