【发布时间】:2015-05-10 02:42:33
【问题描述】:
我有两页
http://site.aspx?page=AddressData2&AddressID=298587,466579,66052
http://site.aspx?page=AddressData2&ShowPanel=EID
第二个链接意味着在第一个链接被访问后保持相同的 cookie/会话信息。
我通过以下方式存储了第一个 cookie:
$cookies =array(
$cookies[0]=>$cookies[1],
"__utma"=>"250300755.603693956.1425821004.1425827777.1425854702.4",
"__utmb"=>"250300755",
"__utmc"=>"250300755",
"__utmz"=>"250300755.1425821004.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none)",
"style"=>"A--"
);
$formattedCookies='';
foreach ($cookies as $key => $value) {
$formattedCookies.=$key."=".$value."; ";
}
$formattedCookies .=" path=/; HttpOnly ";
这与您将在浏览器的 cookie 存储中获得的相同 cookie 非常相似。
但是,在我以与第一个链接相同的方式访问第二个链接后
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER ,1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url_search);
// Execute
$result=curl_exec($ch);
另外,对于访问第二个链接的 curl 调用,我在 curl_setopt($ch, CURLOPT_HEADER ,1); 下添加了这一行来设置我之前制作的 cookie,
curl_setopt($ch2, CURLOPT_COOKIE,$formattedCookies);
然而,结果却截然不同。
在第一个链接中,curl_exec($ch2) 的标头结果如下所示:
HTTP/1.1 302 Found
Date: Sun, 08 Mar 2015 23:28:34 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Location: site.aspx?page=AddressData2&AddressID=298587,466579,66052
Set-Cookie: ASP.NET_SessionId=grrmvt55muepmsqruxxqwfrl; path=/; HttpOnly
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 201
HTTP/1.1 200 OK
Date: Sun, 08 Mar 2015 23:28:34 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Set-Cookie: ASP.NET_SessionId=scmi1gvhrw5pwh45nv0hw1j3; path=/; HttpOnly
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 17170
虽然,在第二个链接中,结果是这样的:
HTTP/1.1 302 Found
Date: Mon, 09 Mar 2015 00:01:19 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Location: site.aspx?page=error
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 156
HTTP/1.1 200 OK
Date: Mon, 09 Mar 2015 00:01:19 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Set-Cookie: ASP.NET_SessionId=dssnqq45hytyvy55kl3na455; path=/; HttpOnly
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 16335
您可以看到 Set-Cookie 和 Location 之间存在很大差异,在第二个链接 curl 结果中,该 Location 成为错误页面,而标题的第一部分不存在 set-cookie。
不知道我哪里做错了?
【问题讨论】:
标签: php curl cookies header web-crawler