$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
我通常不會建議這個,但使用'@'操作符。 '$ result = @file_get_contents($ url);' –
然後確保在使用前檢查結果 – Anigel
正如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)變量如果你想看到錯誤信息。 –