0

情况:

进入站点时,它会设置会话 cookie 并重定向到自身。

问题:

将 curl 与FOLLOW_RELOCATIONand一起使用时COOKIE_SESSION,它不会在重定向后应用 cookie,并最终出现最大重定向限制达到错误。

代码:

curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_INTERFACE, $ip);
curl_setopt($ch, CURLOPT_VERBOSE, 1);

详细输出:

* About to connect() to www.thesite.org port 80 (#0)
*   Trying x.x.x.254... * Name 'x.x.29.61' family 2 resolved to 'x.x.29.61' family 2
* Local port: 0
* connected
* Connected to www.thesite.org (x.x.x.254) port 80 (#0)
> GET / HTTP/1.1
Host: www.thesite.org
Accept: */*

< HTTP/1.1 302 Moved Temporarily
< Date: Wed, 31 Jul 2013 15:14:05 GMT
< Server: Apache
< cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
< pragma: no-cache
< expires: Mon, 17 Jul 1978 05:00:00 GMT
< Last-Modified: Wed, 31 Jul 2013 15:14:05 GMT
< Set-Cookie: JSESSIONID=0A2488FF811CCDAB6A6C95C12AA3F4F8.DB4B9335DF549E1FCF56CF5BD8; Path=/
< Location: http://www.thesite.org/
< Content-Length: 0
< Vary: Accept-Encoding
< Content-Type: text/html
< 
* Connection #0 to host www.thesite.org left intact
* Issue another request to this URL: 'http://www.thesite.org/'
* Re-using existing connection! (#0) with host www.thesite.org
* Connected to www.thesite.org (x.x.x.254) port 80 (#0)
> GET / HTTP/1.1
Host: www.thesite.org
Accept: */*

< HTTP/1.1 302 Moved Temporarily
< Date: Wed, 31 Jul 2013 15:14:06 GMT
< Server: Apache
< cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
< pragma: no-cache
< expires: Mon, 17 Jul 1978 05:00:00 GMT
< Last-Modified: Wed, 31 Jul 2013 15:14:06 GMT
< Set-Cookie: JSESSIONID=5A80785B002D8C37F73F798B75A090B4.47EF37A843F03A8253F26BC644; Path=/
< Location: http://www.thesite.org/
< Content-Length: 0
< Vary: Accept-Encoding
< Content-Type: text/html
< 
* Connection #0 to host www.thesite.org left intact
* Issue another request to this URL: 'http://www.thesite.org/'
* Re-using existing connection! (#0) with host www.thesite.org
* Connected to www.thesite.org (x.x.x.254) port 80 (#0)
> GET / HTTP/1.1
Host: www.thesite.org
Accept: */*

etc...

问题是:

我究竟做错了什么?如何让它发挥作用?当然我可以连接两次并手动设置,但这不是我想做的,这就是我使用CURL而不是file_get_contents().

4

1 回答 1

0

您的代码没有显示您正在使用 cookiefile 来存储页面加载之间的信息。

添加以下几行,它应该会更好地工作。

$cookie_file='cookies.txt'; //Change this to wherever you want.
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
于 2013-07-31T15:38:05.047 回答