在PHP中如何处理file_get_contents()函数的警告? [英] How can I handle the warning of file_get_contents() function in PHP?
问题描述
我写了一个这样的PHP代码
I wrote a PHP code like this
$site="http://www.google.com";
$content = file_get_content($site);
echo $content;
但是当我从 $ site
我收到以下警告:
But when I remove "http://" from $site
I get the following warning:
警告:
file_get_contents(www.google.com)
[function.file-get-contents]:失败
打开流:
Warning: file_get_contents(www.google.com) [function.file-get-contents]: failed to open stream:
我试过尝试
和 catch
,但没有起作用。
I tried try
and catch
but it didn't work.
推荐答案
步骤1:检查返回码: if($ content === FALSE){// handle error here ...}
Step 1: check the return code: if($content === FALSE) { // handle error here... }
步骤2:通过放置 file_get_contents()之前的错误控制运算符(即 @
):
$ content = @file_get_contents($ site);
Step 2: suppress the warning by putting an error control operator (i.e. @
) in front of the call to file_get_contents():
$content = @file_get_contents($site);
这篇关于在PHP中如何处理file_get_contents()函数的警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!