0

这是我的代码:

$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);

此代码作为phpdiffle.org 上的交互式演示

为什么它还在重定向?我正在尝试将重定向到 URL。我设置FOLLOWLOCATIONFALSE仍然。

4

1 回答 1

1

好的,这就是我如何快速调试这些东西(它并不总是有效,但首先尝试在路上打橡胶以获得更多接触,这通常是这样做的):

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

-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,该响应正文由超文本客户端(网络浏览器)解析为Refresh:HTTP 等效标头值。

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

于 2013-08-31T14:38:54.897 回答