2012-10-09 127 views
4

如何解決php警告:file_get_contents?如何解決php警告:file_get_contents?無法打開流:HTTP請求失敗

Warning: file_get_contents(http://192.168.1.254:9999/api/p/internal/ucenter/login?loginName=test102&password=111111) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /Applications/XAMPP/xamppfiles/htdocs/PHP_test/index.php on line 49 

這是關係到$文件中的代碼:

<?php 
$loginNames="test102"; 
$passwords="111111"; 
$apiUrl = "http://192.168.1.254:9999/api/p/internal/ucenter/login?loginName=".$loginNames."&password=".$passwords; 
$callback = file_get_contents($apiUrl); 

print_r($callback); 
//echo $callback; 
?> 
+0

您是否像'$ callback = @file_get_contents($ apiUrl); if($ callback!== false)print_r($ callback);'? – inhan

+0

如果您在瀏覽器中轉到該網址,會發生什麼情況?你是否重定向?嘗試使用CURL並查看標題返回的內容。 – shapeshifter

+0

http://stackoverflow.com/questions/12489499/uploading-remote-url-to-server/12489662#12489662爲相似的CURL代碼。 – shapeshifter

回答

10

如果顯示在您的網頁,有什麼東西不對您display_errors設置。

理想情況下,display_errors應該是Off生產機器,你應該使用set_error_handler()自己處理錯誤。

參見:Error logging, in a smooth way

這種特殊的警告不能停止,除非你確認頁面存在。但是,單獨使用,可以使用muffle operator防止警告出現:

if (false !== ($contents = @file_get_contents('...'))) { 
    // all good 
} else { 
    // error happened 
} 

注意,這仍然將調用自定義錯誤處理程序

0

我對你有更好的選擇。

避免file_get_contents,用戶cURL

讓我告訴你一個例子:

$url = 'http://www.yoururl.com'; 
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_HEADER, false); 
$data = curl_exec($curl); 
curl_close($curl); 

注意:您覺得編碼問題補充一點:curl_setopt($curl, CURLOPT_ENCODING ,"");

感謝。

相關問題