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

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样