简体   繁体   中英

PHP curl headers(?) issue

There is an addon for firefox called httprequester. ( https://addons.mozilla.org/en-US/firefox/addon/httprequester/ )

When I use the addon to send a GET request with a specific cookie, everything works fine.

Request header:

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

Response header:

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

And then I am trying to do the same thing with 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>";

Request header:

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

Response header:

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

I don't know if it has anything to do with my problem, but a thing I noticed is that the first lines of the request headers are different

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

and

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

My problem is that I get 200 http code with the addon and 302 with curl, however I'm sending (or trying to send) the same request.

If i really understand your problem, the thing is cURL is not following the redirect. He don't do that by default, you need to set a option:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

With this, cURL is able to follow the redirects.

To set the Cookies to the request use, (You may need pass the user agent):

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

I think your addon sends the useragent string by default from the browser. If you add useragent string with your curl request, I believe your problem will resolve!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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