dongqin6926 2011-05-31 06:47
浏览 202
已采纳

使用get_meta_tags()和get_headers()进行异常处理?

In PHP, I am using get_meta_tags() and get_headers(), however, when there is a 404, those two functions throw a warning. Is there any way for me to catch it? Thanks!

  • 写回答

3条回答 默认 最新

  • duandang6352 2011-05-31 07:11
    关注

    get_headers does not throw a Warning/Error on 404, but get_meta_tags does.

    So you can check the header response and do something, when it's not OK:

    $url = 'http://www.example.com/';
    
    $headers = array();
    $metatags = array();
    
    $validhost = filter_var(gethostbyname(parse_url($url,PHP_URL_HOST)), FILTER_VALIDATE_IP);
    if($validhost){
        // get headers only when Domain is valid
        $headers = get_headers($url, 1);
    
        if(substr($headers[0], 9, 3) == '200'){
            // read Metatags only when Statuscode OK
            $metatags = get_meta_tags($url);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

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