duanfeng3879 2017-05-24 14:54
浏览 75
已采纳

如何在不使用@的情况下抑制get_headers()中的错误

get_headers() throws a warning if the URL being checked is invalid. E.g.,

get_headers('http://nonexistingrubbish-url.com');

Warning: get_headers(): php_network_getaddresses: getaddrinfo failed: No such host is known

Is it possible to suppress this error withou using @?

My main goal is to check whether a URL exist or not but I don't want to use the @ suppressor.

  • 写回答

3条回答 默认 最新

  • dpbv85276 2017-05-24 15:03
    关注

    You can check with curl and it's not returning any warnings. If you use 'CURLOPT_NOBODY' then it will not try to download whole page.

    <?php
    $url = "http://nonexistingrubbish-url.com";
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_NOBODY, true);
    $result = curl_exec($curl);
    if ($result !== false) {
        $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        if ($statusCode == 404) {
            echo "URL Not Exists";
        } else {
            echo "URL Exists";
        }
    } else {
        echo "URL not Exists";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Converting circular structure to JSON
  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader
  • ¥45 谷歌浏览器出现开发者工具无法显示已创建的,但您可以调试已部署的代码。 状态代码 404, net::ERR HTTP RESPONSE CODE FAILURE
  • ¥15 如何解决蓝牙通话音频突发失真问题
  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas