PHP cURL:HTTP头显示302和cookie集,cookie被保存和发送,同样的头出现? [英] PHP cURL: HTTP headers show 302 and cookies set, cookies are saved and sent, same headers appear?

查看:699
本文介绍了PHP cURL:HTTP头显示302和cookie集,cookie被保存和发送,同样的头出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个从昨天的问题继续:似乎无法通过cURL获取网页的内容 - 用户代理和HTTP标头都设置了吗?



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



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



第一次请求(无Cookie) strong>



在学习后在命令行中使用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
服务器: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,2012年1月27日
13:15:00 GMT; Max-Age = 2419200; Path = /
位置: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): p>

因此,我使用 -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
日期:Fri,30 Dec 2011 13:38:17 GMT
服务器:Apache / 2.2.16(Debian)
Vary:Accept-语言,Cookie,接受编码
Content-Language:de
Set-Cookie:csrftoken = 49f5c804d399f8581253630631692f5f; Domain = www.deindeal.ch; Max-Age = 31449600; P
ath = /
位置:http://www.deindeal.ch/welcome/?deal_slug=atlas-grand-hotel-2-naechte-30-2
Content-Length: 0
连接:close
Content-Type:text / html; charset = utf-8

对我来说,这看起来是完全相同的内容,减去一个或两个参数



我试图让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事务使用 -c 标志后用于保存cookie的文件。



使用 WebGet (对不起,我写的)拉取内容很简单。

  requireWebGet.php ; 
$ w = new WebGet();
$ w-> cookieFile ='cookie.txt'; // must be writable
$ w-> requestContent(https://github.com/shiplu/dxtool);
print_r($ w-> responseHeaders)//打印响应头
print_r($ w-> cachedContent)//打印url内容


This is kind of a carry on from a question asked yesterday: Can't seem to get a web page's contents via cURL - user agent and HTTP headers both set?

I'm attempting to access a url's contents, the problem is the way this url handles request.

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

First request (without cookies):

After "learning" to use curl in the command line (props to @d3v3us), a simple request curl -i http://www.deindeal.ch/deals/atlas-grand-hotel-2-naechte-30-2/ shows the following:

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

Second request (with cookies):

So, I save the cookie using -c, check that it saves as cookie.txt, and run the request again with the addition of -b cookie.txt, getting this:

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

To me this looks like exactly the same contents, minus one or two parameters in the cookie, but maybe I'm overlooking something?

I'm attempting to get the curl request to function and return the same contents as when requesting that url via a browser, but I'm not sure what I should do next.

Note: I've tagged this PHP, as I am using PHP to make the requests, I've simply using command line to easily show the returned headers - so if there's any other PHP libraries or methods that would work (better, or in a place that cURL wouldn't), please feel free to suggest any.

Any help would be greatly appreciated ;).

解决方案

You need this,

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

-b flag is used to read cookie from . For a file to be used to save cookie after the http transaction use -c flag. Its called cookie jar.

Using WebGet (Sorry, Its written by me) pulling the contents is quite simple.

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

这篇关于PHP cURL:HTTP头显示302和cookie集,cookie被保存和发送,同样的头出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆