这个debug-verbose-info是什么意思?

时间:2016-09-16 14:02:19

标签: php debugging curl

我尝试通过cURL + PHP获取页面内容,但它没有给我任何回报。当我用google.com替换网址时,它可以正常工作。

请求的页面是受htaccess保护的

这是我的PHP代码

$login = 'admin';
$password = 'xxxxx';

$ch = curl_init();        
curl_setopt($ch, CURLOPT_URL, $_REQUEST['url']);      
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);    

curl_setopt($ch, CURLOPT_VERBOSE, true);

$verbose = fopen('bla.txt', 'w+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");

$output = curl_exec($ch);        
curl_close($ch);  

echo $output;

这是详细信息:

* Hostname was NOT found in DNS cache
*   Trying xxx.xxx.xxx.xxx...
* Connected to xxxxxxxxx (xxx.xxx.xxx.xxx) port 80 (#0)
* Server auth using Basic with user 'admin'
> GET /mypage.php HTTP/1.1

Authorization: Basic YWRtaW46cXdlcnR6dTE=

Host: xxxxxxxxxxxxxx.de

Accept: */*



< HTTP/1.1 301 Moved Permanently

< Date: Fri, 16 Sep 2016 13:44:28 GMT

* Server Apache is not blacklisted
< Server: Apache

< X-Powered-By: PHP/5.4.45

< Expires: Thu, 19 Nov 1981 08:52:00 GMT

< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0

< Pragma: no-cache

< Set-Cookie: PHPSESSID=23cd31457358a63a1b32b86992e906bf2; path=/; HttpOnly

< Location: xxxxxxxxxxxxxxxxxxxxxxx

< Content-Length: 0

< Connection: close

< Content-Type: text/html; charset=UTF-8

< 

* Closing connection 0
有人可以告诉我什么是错的吗?

3 个答案:

答案 0 :(得分:2)

cURL正在停止,因为就其而言,工作已经完成。它已获取所请求的页面。您看到的响应是301永久重定向标头。如果您在浏览器中访问了最初为cURL请求指定的URL,则会自动将URL跟踪到指定的目标。 cURL不会自动遵循重定向。

您可能想要使用CURLOPT_FOLLOWLOCATION选项。 The manual将此描述为:

  

设置为1的long参数告诉库遵循服务器在3xx响应中作为HTTP标头的一部分发送的任何Location:标头。 Location:标头可以指定要遵循的相对或绝对URL。

您可以在PHP中实现它:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

此cURL选项的the documentation

如果您不想使用此选项,您还可以通过获取301 HTTP状态代码响应中指定的位置并将其用作您的URL来手动重定向您的页面。

答案 1 :(得分:1)

HTTP状态代码301表示您尝试获取内容的网页的网址已移至新网址。您无法使用旧网址检索此网站的内容,但您已收到通知,现在可通过重定向网址访问该网站。

如果可能,请通过导航(通过浏览器)到旧网址获取重定向网址,并查看重定向到的位置。然后在此行的curl中使用这个新的重定向URL:

curl_setopt($ch, CURLOPT_URL, $newURL);  

答案 2 :(得分:1)

尝试添加CURLOPT_FOLLOWLOCATION并详细了解CURLOPT_FOLLOWLOCATION和safe_mode:https://stackoverflow.com/a/21234822/6797531

相关问题