这是昨天提出的问题:Can't seem to get a web page's contents via cURL - user agent and HTTP headers both set?
我正在尝试访问网址的内容,问题在于此网址处理请求的方式。
网址: http://www.deindeal.ch/deals/atlas-grand-hotel-2-naechte-30-2/
首次请求(不含Cookie):
在“学习”命令行中使用curl(对@ 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
第二次请求(使用Cookie):
所以,我使用-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不会的地方),请随时提出建议。
非常感谢任何帮助;)。
答案 0 :(得分:1)
你需要这个,
curl -iL -c cookie.txt -b cookie.txt http://www.deindeal.ch/deals/atlas-grand-hotel-2-naechte-3
-b
标志用于从中读取cookie。对于在http事务使用-c
标志后用于保存cookie的文件。它叫做 cookie jar 。
使用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
答案 1 :(得分:0)
我可能误解了您的问题,但302响应意味着找到了内容,您只需要按照“位置”进行操作即可? cUrl只会执行一个请求,不像你的浏览器会看到302(设置cookie,就像你正在做的那样),然后按照该位置标题。看起来你的位置有一个“?”在它里面不是原版。在位置网址上使用相同的Cookie jar运行cUrl。
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection