file_get_contents()
函數(shù)在 PHP 中用于從文件或 URL 讀取內(nèi)容
@
符號(hào)來(lái)抑制錯(cuò)誤信息:$content = @file_get_contents('http://example.com');
if ($content === false) {
// 處理錯(cuò)誤
} else {
// 處理正常情況
}
trigger_error()
自定義錯(cuò)誤處理:function custom_error_handler($errno, $errstr, $errfile, $errline) {
// 在這里處理錯(cuò)誤,例如記錄日志、發(fā)送通知等
}
set_error_handler("custom_error_handler");
$content = file_get_contents('http://example.com');
if ($content === false) {
trigger_error("Error reading from URL", E_USER_WARNING);
} else {
// 處理正常情況
}
class FileGetContentsException extends Exception {}
function get_url_content($url) {
$content = file_get_contents($url);
if ($content === false) {
throw new FileGetContentsException("Error reading from URL: " . $url);
}
return $content;
}
try {
$content = get_url_content('http://example.com');
// 處理正常情況
} catch (FileGetContentsException $e) {
// 處理錯(cuò)誤
echo $e->getMessage();
}
function get_url_content($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
if (curl_errno($ch)) {
// 處理錯(cuò)誤
echo 'Error: ' . curl_error($ch);
} else {
// 處理正常情況
return $content;
}
curl_close($ch);
}
$content = get_url_content('http://example.com');
以上是處理 file_get_contents()
錯(cuò)誤的一些建議。你可以根據(jù)實(shí)際需求選擇合適的方法。