PHP curl headers(?) 问题

PHP curl headers(?) issue

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

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

请求header:

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

回复header:

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>";

请求header:

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

回复header:

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

我不知道这是否与我的问题有关,但我注意到的一件事是请求的第一行 header 是不同的

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

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

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

如果我真的理解你的问题,那就是 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");

该页面正在进行一些重定向,因此您必须关注它

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

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

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"
));