检查无法打开流

时间:2014-11-14 09:26:11

标签: php

我有一段代码,如下所示:

if(strpos(strtolower($preview_url),"https://play.google.com/")!==false)
            {
                $dom = new DOMDocument;
                libxml_use_internal_errors(true);
                $dom->loadHTMLFile($preview_url);
                libxml_clear_errors();
                $xp = new DOMXPath($dom);
                $image_src = $xp->query("//*[@class='cover-image']");
                $cover_image_src = $image_src->item(0)->getAttribute('src');
                echo $cover_image_src."-- ID --".$id."\n";
                $update_offer_sql = "UPDATE aw_offers_v2 SET 
                        name = '$name', description = '$description', 
                        payout_type = '$payout_type', payout = '$payout',
                        expiration_date = '$expiration_time', 
                        preview_url = '$preview_url',tracking_url = '$tracking_url', 
                        categories = '$categories', countries = '$countries',
                        countries_short = '$countries_short', 
                        update_date = '$update_time', api_key = '$api',
                        network_id = '$api_url',
                        icon = '".mysql_real_escape_string(file_get_contents($cover_image_src))."'
                        WHERE id = '$id'";
            }

但有时候,如果$ preview_url是一个不存在的网址,那我就得到了

DOMDocument::loadHTMLFile(https://play.google.com/store/apps/details?id=com.mobogenie.markets): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found

如何在那里显示自定义消息,使用php

查看和打开消息

1 个答案:

答案 0 :(得分:2)

检查是否为404

来自here

$file_headers = @get_headers($preview_url);
if($file_headers[0] == 'HTTP/1.1 404 Not Found' || $file_headers[0] == 'HTTP/1.0 404 Not Found') {
    $exists = false;
}
else {
    $exists = true;
}

检查给定的网址是否正常

使用filter_var

if (filter_var($preview_url, FILTER_VALIDATE_URL) === false){
  echo "There is an error :(";
}