我写了一个像这样的 PHP 代码
$site="http://www.google.com";
$content = file_get_content($site);
echo $content;
但是当我从$site
中删除"http://"时,我会收到以下警告:
警告:file_get_contents(www.google.com)[function.file-get-contents]:无法打开流:
我尝试了try
和catch
,但没有起作用。
我写了一个像这样的 PHP 代码
$site="http://www.google.com";
$content = file_get_content($site);
echo $content;
但是当我从$site
中删除"http://"时,我会收到以下警告:
警告:file_get_contents(www.google.com)[function.file-get-contents]:无法打开流:
我尝试了try
和catch
,但没有起作用。
步骤1:检查返回代码:if($content === FALSE) { // 在此处理错误... }
步骤2:通过在调用file_get_contents()之前放置错误控制运算符(即@
)来抑制警告:
$content = @file_get_contents($site);
@
符号可能会对性能产生负面影响。可以参考一个相关帖子上的答案,解释得相当清楚:https://dev59.com/7nI-5IYBdhLWcg3wI0p9#2002789。 - Fr0zenFyrset_error_handler(
function ($severity, $message, $file, $line) {
throw new ErrorException($message, $severity, $severity, $file, $line);
}
);
try {
file_get_contents('www.google.com');
}
catch (Exception $e) {
echo $e->getMessage();
}
restore_error_handler();
我最喜欢的方法相当简单:
if (($data = @file_get_contents("http://www.google.com")) === false) {
$error = error_get_last();
echo "HTTP request failed. Error was: " . $error['message'];
} else {
echo "Everything went better than expected";
}
在尝试使用@enobrev上面提到的try/catch
后,我发现了这个方法,但它可以让代码变得更简洁(并且在我看来更易读)。我们只需要使用error_get_last
获取最后一个错误的文本,而file_get_contents
在失败时返回false,因此一个简单的“if”语句就可以捕获这个错误。
@file_get_contents
来抑制向浏览器报告错误。 - EDP@file_get_contents
来抑制警告并使用=== FALSE
测试结果值,那就更好了。 - kostixif (false !== ($data = file_get_contents ()))
的方式进行判断。 - GordonMerror_get_last
函数返回空值。 - Glenn Schmidt您可以在前面添加 @:
$content = @file_get_contents($site);
这将抑制任何警告 - 请谨慎使用! 详情请见错误控制运算符
编辑:当您删除“http://”时,您不再寻找网页,而是寻找名为“www.google.....”的磁盘文件。
一种替代方案是抑制错误并同时抛出异常,这样您可以稍后捕获它。如果您的代码中有多个对file_get_contents()的调用,这尤其有用,因为您不需要手动抑制和处理所有这些调用。相反,可以在单个try / catch块中对此函数进行多次调用。
// Returns the contents of a file
function file_contents($path) {
$str = @file_get_contents($path);
if ($str === FALSE) {
throw new Exception("Cannot access '$path' to read contents.");
} else {
return $str;
}
}
// Example
try {
file_contents("a");
file_contents("b");
file_contents("c");
} catch (Exception $e) {
// Deal with it.
echo "Error: " , $e->getMessage();
}
function custom_file_get_contents($url) {
return file_get_contents(
$url,
false,
stream_context_create(
array(
'http' => array(
'ignore_errors' => true
)
)
)
);
}
if( $content = custom_file_get_contents($url) ) {
//play with the result
}
else {
//handle the error
}
$url
是404未找到,警告仍然会出现。 - Raptorignore_errors
仅指示HTTP上下文不将状态码大于等于400的HTTP响应解释为错误。虽然有些相关,但这并不能回答关于PHP错误处理的问题。 - sunignore_errors
选项!这正是我所需要的! - Modder这是我是如何做到的... 不需要try-catch块... 最好的解决方案总是最简单的... 享受吧!
$content = @file_get_contents("http://www.google.com");
if (strpos($http_response_header[0], "200")) {
echo "SUCCESS";
} else {
echo "FAILED";
}
$http_response_header
没有被更新,因为没有收到HTTP响应。 - Nathan Reed$this->response_body = @file_get_contents($this->url, false, $context);
if ($this->response_body === false) {
$error = error_get_last();
$error = explode(': ', $error['message']);
$error = trim($error[2]) . PHP_EOL;
fprintf(STDERR, 'Error: '. $error);
die();
}
自 PHP 4 开始使用 error_reporting():
$site="http://www.google.com";
$old_error_reporting = error_reporting(E_ALL ^ E_WARNING);
$content = file_get_content($site);
error_reporting($old_error_reporting);
if ($content === FALSE) {
echo "Error getting '$site'";
} else {
echo $content;
}