![](/img/trans.png)
[英]Cookie issue in CURL (PHP) - Cookie information not attached in 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.