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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度