PHP curl header(?)问题

时间:2015-12-30 10:54:13

标签: php curl header

firefox有一个名为httprequester的插件。 (https://addons.mozilla.org/en-US/firefox/addon/httprequester/

当我使用插件发送带有特定cookie的GET请求时,一切正常。

请求标题:

GET https://store.steampowered.com/account/
Cookie: steamLogin=*removed because of obvious reasons*

响应标题:

200 OK
Server:  Apache
... (continued, not important)

然后我试图用cURL做同样的事情:

$ch = curl_init("https://store.steampowered.com/account/");
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: steamLogin=*removed because of obvious reasons*"));
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
$response = curl_exec($ch);
$request_header = curl_getinfo($ch, CURLINFO_HEADER_OUT);

echo "<pre>$request_header</pre>";
echo "<pre>$response</pre>";

请求标题:

GET /account/ HTTP/1.1
Host: store.steampowered.com
Accept: */*
Cookie: steamLogin=*removed because of obvious reasons*

响应标题:

HTTP/1.1 302 Moved Temporarily
Server: Apache
... (continued, not important)

我不知道它是否与我的问题有任何关系,但我注意到的一点是请求标题的第一行是不同的

GET https://store.steampowered.com/account/

GET /account/ HTTP/1.1
Host: store.steampowered.com

我的问题是我使用插件获得200个http代码,使用curl获得302,但是我正在发送(或尝试发送)相同的请求。

3 个答案:

答案 0 :(得分:1)

如果我真的了解你的问题,那就是cURL没有遵循重定向。默认情况下,他不这样做,你需要设置一个选项:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

有了这个,cURL就能够跟踪重定向。

要将Cookie设置为请求使用,(您可能需要传递用户代理):

curl_setopt($ch, CURLOPT_COOKIE, "Cookie: steamLogin=*removed because of obvious reasons*; User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0");

答案 1 :(得分:1)

页面正在进行重定向,因此您必须遵循它

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

答案 2 :(得分:0)

我认为您的插件默认从浏览器发送useragent字符串。如果您在curl请求中添加useragent字符串,我相信您的问题将会解决!

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Cookie: steamLogin=*removed because of obvious reasons*",
    "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0"
));