2012-12-19 38 views
0
http://[email protected]#$%^&*().com 

這似乎是一個無效的URL,實際上瀏覽器說它是一個無效的URL。一個URL的奇怪結果

但是當我通過file_get_contents(XAMPP)獲取這個URL時,它會給出一個「500 Internal Server Error」異常,因爲這個URL不存在,爲什麼我沒有404?

要檢查我使用

$http_response_header 

這裏的響應是我的代碼:

$url = "http://[email protected]#$%^&*().com"; 
$contents = @file_get_contents($url); 
print_r($http_response_header); 

當我運行另一臺機器(WAMP)上是相同的,那麼它說$http_response_header是一個未定義變量。

任何人都知道這裏有什麼問題?

+1

它會自動填充響應標頭。它適用於其他有效的URL並返回正確的響應。 http://php.net/manual/en/reserved.variables.httpresponseheader.php – Arfeen

回答

1

你是suppres唱在file_get_contents調用錯誤,你輸入的域名實際上是無效的,因爲你說因爲域名無效,函數調用將返回false並觸發以下警告

file_get_contents(http://[email protected]#$%^&*().com): failed to open stream: operation failed 

你不會得到一個404 http請求可能永遠不會發送,因此您的$http_response_header爲空。

XAMPP和WAMP之間OS或PHP版本的差異可能解釋了爲什麼它們的行爲不同?

我的建議是首先檢查file_get_contents的返回值,只有當它不是false時才繼續檢查響應頭。

+0

你是對的,但想在這裏添加一點。正在填充$ http_response_header的計算機也使用XAMPP運行IIS,因此對於這些類型的URL,X-POWERED的值始終是IIS和ASP.NET – Arfeen

+1

好的我得到了它。$ http_response_header實際上顯示了結果最後一次呼叫,如果URL無效。 – Arfeen

0

您沒有收到404,因爲該域不存在。嘗試使用有效的域名。使用http://[email protected]#$%^&*().com將返回failed to open stream: operation failed而有效的域將返回failed to open stream: HTTP request failed

請注意:一個是operation failed和其他HTTP request failed你只能得到HTTP錯誤時,有HTTP請求失敗

error_reporting(E_ALL); 
ini_set("display_errors", "On"); 

$url = "http://stockoverflow.com/xxxx"; // URL does not exist 
$response = @file_get_contents($url); 
var_dump($response,$http_response_header); 

輸出

boolean false 
array (size=7) 
    0 => string 'HTTP/1.1 404 Not Found' (length=22) <--- you get your 404 
    1 => string 'Content-Type: text/html' (length=23) 
    2 => string 'Server: Microsoft-IIS/7.5' (length=25) 
    3 => string 'X-Powered-By: ASP.NET' (length=21) 
    4 => string 'Date: Wed, 19 Dec 2012 10:26:20 GMT' (length=35) 
    5 => string 'Connection: close' (length=17) 
    6 => string 'Content-Length: 1245' (length=20)