PHP cURL: HTTP头显示302和cookie设置,cookie保存和发送,相同的头出现


PHP cURL: HTTP headers show 302 and cookies set, cookies are saved and sent, same headers appear?

这是昨天问的一个问题的延续:似乎不能通过cURL -用户代理和HTTP头都设置获得网页的内容吗?

我试图访问一个url的内容,问题是这个url处理请求的方式。

url: http://www.deindeal.ch/deals/atlas-grand-hotel-2-naechte-30-2/

第一个请求(没有cookie):

在"学习"在命令行中使用curl (props to @d3v3us)之后,一个简单的请求curl -i http://www.deindeal.ch/deals/atlas-grand-hotel-2-naechte-30-2/显示如下:

curl -i http://www.deindeal.ch/deals/atlas-grand-hote
l-2-naechte-30-2/
HTTP/1.1 302 FOUND
Date: Fri, 30 Dec 2011 13:15:00 GMT
Server: Apache/2.2.16 (Debian)
Vary: Accept-Language,Cookie,Accept-Encoding
Content-Language: de
Set-Cookie: csrftoken=edc8c77fc74f5e788c53488afba4e50a; Domain=www.deindeal.ch;
Max-Age=31449600; Path=/
Set-Cookie: generic_cookie=1; Path=/
Set-Cookie: sessionid=740a8a2cb9fb51166dcf865e35b91888; expires=Fri, 27-Jan-2012
 13:15:00 GMT; Max-Age=2419200; Path=/
Location: http://www.deindeal.ch/welcome/?deal_slug=atlas-grand-hotel-2-naechte-
30-2
Content-Length: 0
Connection: close
Content-Type: text/html; charset=utf-8

第二个请求(饼干):

因此,我使用-c保存cookie,检查它是否保存为cookie.txt,并添加-b cookie.txt再次运行请求,得到以下内容:

curl -i -b cookie.txt http://www.deindeal.ch/deals/atlas-grand-hotel-2-naechte-3
0-2/
HTTP/1.1 302 FOUND
Date: Fri, 30 Dec 2011 13:38:17 GMT
Server: Apache/2.2.16 (Debian)
Vary: Accept-Language,Cookie,Accept-Encoding
Content-Language: de
Set-Cookie: csrftoken=49f5c804d399f8581253630631692f5f; Domain=www.deindeal.ch; Max-Age=31449600; P
ath=/
Location: http://www.deindeal.ch/welcome/?deal_slug=atlas-grand-hotel-2-naechte-30-2
Content-Length: 0
Connection: close
Content-Type: text/html; charset=utf-8

对我来说,这看起来完全相同的内容,减去一个或两个参数在cookie中,但也许我忽略了什么?

我试图得到curl请求的功能,并返回相同的内容时,通过浏览器请求该url,但我不确定我应该做下一步。

注意:我已经标记了这个PHP,因为我使用PHP来发出请求,我只是简单地使用命令行来轻松显示返回的头-所以如果有任何其他PHP库或方法可以工作(更好,或者在cURL不能的地方),请随时提出建议。

你需要这个,

curl -iL  -c cookie.txt -b cookie.txt http://www.deindeal.ch/deals/atlas-grand-hotel-2-naechte-3

-b标志用于读取cookie。对于在http事务之后用于保存cookie的文件,使用-c标志。它叫饼干罐

使用WebGet(对不起,这是我写的)提取内容非常简单。

require "WebGet.php";
$w = new WebGet();
$w->cookieFile = 'cookie.txt'; // must be writable
$w->requestContent("https://github.com/shiplu/dxtool");
print_r($w->responseHeaders) // prints response headers
print_r($w->cachedContent) // prints url content

我可能误解了你的问题,但是302响应意味着找到了内容,你只需要遵循"位置"对吗?cUrl只会执行一个请求,不像你的浏览器会看到302(设置cookie,就像你正在做的那样),然后跟随那个位置标头。看起来你的位置中有一个"?"在原始位置中没有。使用相同的cookie jar,在Location url上运行cUrl。

http://en.wikipedia.org/wiki/List_of_HTTP_status_codes # 3 xx_redirection