2016-09-16 126 views
-2

我尝试通过cURL + PHP获取页面的内容,但它没有给我回复。当我用google.com代替URL时,它可以工作。这个debug-verbose-info是什么意思?

请求的页面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 

谁能告诉我什么是错? ?

回答

1

HTTP状态码301意味着您尝试获取内容的页面的URL已移至新的URL。您无法使用旧网址检索本网站的内容,但您现在已通知该网站可通过重定向网址进行访问。

如果可能,通过导航(通过浏览器)到旧URL并查看重定向到的位置来获取重定向URL。然后在你的卷发使用这个新的,重定向的URL在这一行:

curl_setopt($ch, CURLOPT_URL, $newURL); 
2

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

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

长参数设置为1告诉库遵循任何地点:报头,该服务器发送如在3xx应答HTTP报头的一部分。 Location:标题可以指定一个相对或绝对URL。

你会实现它在PHP这样的:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 

这里是the documentation这个卷曲选项。


如果你不想使用此选项,你也可以手动通过采取在301 HTTP状态代码响应指定的位置,并以此作为你的网址,而不是重定向页面。