用它的语句处理php错误


Handle php errors with is statment

我想要一个if语句检查php脚本的特定错误错误如下

Warning: file_get_contents(http://www.google.com): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:'xampp'htdocs'google'index.php on line 196

我的代码
if (file_get_contents('http://www.google.com') === false {
   echo 'faild';  
}

我只想跳过404错误

ps:我没有使用我的代码在谷歌网站,但我不能写真正的url

(抱歉我的英语不好)

您可以尝试添加一个错误处理程序并解析错误消息以检查404 Not Found或检查http代码,然后执行file_get_contents

可以使用php curl获取远程页面。它允许您检查http状态码。参见示例2 http://php.net/manual/en/function.curl-getinfo.php