将FOLLOWLOCATION设置为FALSE后,卷曲仍会重定向

时间:2013-08-30 20:24:50

标签: php redirect curl

这是我的代码:

$url='http://celebcrust.com/?p=15055';

$ch = curl_init();

curl_setopt($ch, CURLOPT_COOKIESESSION,  TRUE);
curl_setopt($ch, CURLOPT_URL,            $url);
curl_setopt($ch, CURLOPT_HEADER,         TRUE);                 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);

$httpData = curl_exec($ch);
var_export($httpData);

此代码为interactive demo on phpdiffle.org

为什么还在重定向?我正在尝试将重定向到URL。我将FOLLOWLOCATION设置为FALSE,但仍然。

1 个答案:

答案 0 :(得分:1)

好的,这就是我如何快速调试这些东西(它总是不能正常工作,但是首先尝试在路上打橡胶以获得更多联系,通常这样做):

要求:命令行的卷曲(可能适用于地球上的每个计算机系统,如果您还没有,请访问主页):

-i也列出标题(如果数据太多,则使用-I表示HEAD请求),然后-v表示详细信息(显示其中的内容):

$ curl -iv 'http://celebcrust.com/?p=15055'
* Adding handle: conn: 0xa50260
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0xa50260) send_pipe: 1, recv_pipe: 0
* About to connect() to celebcrust.com port 80 (#0)
*   Trying 70.32.78.224...
* Connected to celebcrust.com (70.32.78.224) port 80 (#0)
> GET /?p=15055 HTTP/1.1
> User-Agent: curl/7.30.0
> Host: celebcrust.com
> Accept: */*
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Date: Sat, 31 Aug 2013 14:29:54 GMT
Date: Sat, 31 Aug 2013 14:29:54 GMT
* Server Apache is not blacklisted
< Server: Apache
Server: Apache
< X-Pingback: http://celebcrust.com/xmlrpc.php
X-Pingback: http://celebcrust.com/xmlrpc.php
< X-Powered-By: PleskLin
X-Powered-By: PleskLin
< Content-Length: 159
Content-Length: 159
< Connection: close
Connection: close
< Content-Type: text/html; charset=UTF-8
Content-Type: text/html; charset=UTF-8

<
<META HTTP-EQUIV=Refresh CONTENT="0; URL=http://www.celebgossip.com/2013/04/willie-nelson-celebrates-80th-birthday-stoned-and-auditi
oning-for-gandalf-39425/">
* Closing connection 0

因为这表明服务器没有发送Location:标题,所以这完全解释了你没有看到它。

相反,它会在响应主体中发送HTML,该主体由超文本客户端(webbrowser)解析,以获得Refresh: HTTP等效标头值。

这不是卷曲的商业。您需要添加一个HTML解析器并检查这些,我建议DOMDocument使用->loadHTML()方法。