【问题标题】:Handle php errors with is statment使用 if 语句处理 php 错误
【发布时间】:2017-02-17 06:58:16
【问题描述】:

我想要一个 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:我没有在谷歌网站上使用我的代码,但我不能写真正的网址

(对不起我的英语不好)

【问题讨论】:

  • 您无法使用 file_get_contents(), this function will return true` 或 false 检查 404 错误。您最好的关闭是使用.htacces 文件,设置条件ErrorDocument 404 /404.html end 然后创建404.html page which will automatically display if a 404` 错误发生。

标签: php


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在执行 file_get_contents 之前,您可以尝试 add a error handler 并解析错误消息以检查 404 Not Foundcheck the http code

    【讨论】:

    • 刚刚完美的thanx
    猜你喜欢
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    相关资源
    最近更新 更多