繁体   English   中英

PHP curl headers(?)问题

[英]PHP curl headers(?) issue

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个,但是我正在发送(或尝试发送)相同的请求。

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

我认为您的插件默认会从浏览器发送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"
));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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