2016-09-16 89 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狀態代碼響應指定的位置,並以此作爲你的網址,而不是重定向頁面。