php file_get_contents 警告,如何在PHP中处理file_get_contents()函数的警告?

这篇博客讨论了如何在PHP中处理file_get_contents()函数产生的警告。当从URL中移除'http://'时,会触发警告。建议的方法包括使用错误控制运算符@来抑制警告,设置自定义错误处理器将错误转换为异常,或者检查HTTP响应码来确认请求是否成功。此外,还提到了设置allow_url_fopen和allow_url_include选项的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何在PHP中处理file_get_contents()函数的警告?

我写了这样的PHP代码

$site="http://www.google.com";

$content = file_get_content($site);

echo $content;

但是当我从try删除“[http://”]时,我收到以下警告:

警告:  的file_get_contents(www.google.com)   [function.file-get-contents]:失败   打开流:

我试过try和catch,但它没有用。

16个解决方案

435 votes

第1步:检查返回码:@

步骤2:通过在调用file_get_contents()之前放置一个错误控制操作符(即@)来抑制警告:$content = @file_get_contents($site);

Roel answered 2019-02-19T18:02:52Z

117 votes

您还可以将错误处理程序设置为匿名函数,该函数调用Exception并对该异常使用try / catch。

set_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();

似乎有很多代码可以捕获一个小错误,但如果你在整个应用程序中使用异常,你只需要在顶部(例如,在一个包含的配置文件中)执行此操作,它将会 将所有错误转换为异常。

enobrev answered 2019-02-19T18:03:26Z

55 votes

我最喜欢这样做的方法很简单:

if (!$data = file_get_contents("http://www.google.com")) {

$error = error_get_last();

echo "HTTP request failed. Error was: " . $error['message'];

} else {

echo "Everything went better than expected";

}

我在上面的@enobrev中试验了try/catch之后发现了这个,但这样可以减少冗长(和IMO,更易读)的代码。 我们只是使用error_get_last来获取最后一个错误的文本,并且file_get_contents在失败时返回false,因此一个简单的“if”可以捕获它。

Laurie answered 2019-02-19T18:04:01Z

31 votes

您可以添加@:$content = @file_get_contents($site);

这将禁止任何警告 - 谨慎使用! 请参见错误控制操作符

编辑:当您删除'[http://']时,您不再需要网页,而是磁盘上名为“www.google .....”的文件

Greg answered 2019-02-19T18:04:44Z

15 votes

另一种方法是抑制错误,并抛出一个稍后可以捕获的异常。 如果在代码中多次调用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();

}

Aram Kocharyan answered 2019-02-19T18:05:13Z

12 votes

这是我如何做到的......不需要试试块...最好的解决方案始终是最简单的...享受!

$content = @file_get_contents("http://www.google.com");

if (strpos($http_response_header[0], "200")) {

echo "SUCCESS";

} else {

echo "FAILED";

}

answered 2019-02-19T18:05:39Z

12 votes

function custom_file_get_contents($url) {

return file_get_contents(

$url,

false,

stream_context_create(

array(

'http' => array(

'ignore_errors' => true

)

)

)

);

}

$content=FALSE;

if($content=custom_file_get_contents($url)) {

//play with the result

} else {

//handle the error

}

RafaSashi answered 2019-02-19T18:05:59Z

6 votes

这是我如何处理:

$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();

}

Jrm answered 2019-02-19T18:06:26Z

3 votes

最好的方法是设置自己的错误和异常处理程序,这些处理程序将执行一些有用的操作,例如将其记录在文件中或通过电子邮件发送关键文件。[http://www.php.net/set_error_handler]

answered 2019-02-19T18:06:53Z

1 votes

你可以使用这个脚本

$url = @file_get_contents("http://www.itreb.info");

if ($url) {

// if url is true execute this

echo $url;

} else {

// if not exceute this

echo "connection error";

}

ogie answered 2019-02-19T18:07:24Z

1 votes

从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;

}

Bob Stein answered 2019-02-19T18:07:50Z

-2 votes

这将尝试获取数据,如果它不起作用,它将捕获错误并允许您在catch中执行任何您需要的操作。

try {

$content = file_get_contents($site);

} catch(\Exception $e) {

return 'The file was not found';

}

Brad answered 2019-02-19T18:08:17Z

-3 votes

更改文件php.ini

allow_url_fopen = On

allow_url_include = On

Antério Vieira answered 2019-02-19T18:08:45Z

-3 votes

您应该先使用file_exists()函数来使用file_get_contents()。通过这种方式,您将避免PHP警告。

$file = "path/to/file";

if(file_exists($file)){

$content = file_get_contents($file);

}

Jesús Díaz answered 2019-02-19T18:09:13Z

-3 votes

try {

$site="http://www.google.com";

$content = file_get_content($site);

echo $content;

} catch (ErrorException $e) {

// fix the url

}

set_error_handler(function ($errorNumber, $errorText, $errorFile,$errorLine )

{

throw new ErrorException($errorText, 0, $errorNumber, $errorFile, $errorLine);

});

kta answered 2019-02-19T18:09:32Z

-3 votes

你也应该设置

allow_url_use = On

在你的php.ini停止接收警告。

GHAV answered 2019-02-19T18:10:09Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值