2013-07-22 113 views
0
$url = 'xxxxxxxxx'; 
$result = file_get_contents($url); 
$stuff = json_decode($result, true); 
if (false !== ($contents = $stuff)) { 
echo $contents; 
} else { 
//do nothing 
} 

如果查詢犯規返回任何結果,我想沒有什麼表現的file_get_contents忽略錯誤

相反,我得到

Warning: file_get_contents(xxxx) [function.file-get-contents]: 
failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /home/xxxxx/xxx.php on line 4 
+4

我通常不會建議這個,但使用'@'操作符。 '$ result = @file_get_contents($ url);' –

+0

然後確保在使用前檢查結果 – Anigel

+0

正如Rocket所說的,在函數名前使用'@',並使用[error_get_last](http:// www.php.net/manual/es/function.error-get-last.php)函數或[$ php_errormsg](http://www.php.net/manual/es/reserved.variables.phperrormsg.php)變量如果你想看到錯誤信息。 –

回答

1

函數之前添加@或設置error_reporting(0)(不是因爲所有推薦錯誤將被抑制)。

+0

在哪裏設置它。新的php – Kundan

+0

我想你是指Catch錯誤。嘗試...趕上建設。 –

+1

我不會建議設置error_reporting(0),因爲它會阻止所有顯示出來的錯誤,IE會隱藏症狀而不能解決問題。按照Rocket Hazmat的建議,你最好使用@,至少你會看到其他發生的錯誤 – Anigel