发布日期:2018-03-26
PHP中如何处理关于file_get_contents() 函数的警告?+ 查看更多
PHP中如何处理关于file_get_contents() 函数的警告?
+ 查看更多
发布日期:2018-02-24 17:53
分类:PHP
浏览次数:50
我写了如下的PHP代码:
$site="http://www.google.com"; $content = file_get_content($site); echo $content;
但是当我从$site中删除“http://”时,我得到了如下的警告提示:
Warning: file_get_contents(www.google.com) [function.file-get-contents]: failed to open stream:
我尝试使用try and catch 捕获异常但是不起作用。
回答:
第一步:检查返回的代码:
if($content === FALSE) { // handle error here... }第二步:通过在file_get_contents前添加一个@来阻止警告提示:
$content = @file_get_contents($site);