繁体   English   中英

当file_get_content错误的URL时发出警告

[英]Warnings when file_get_content wrong url

我有以下代码:

<?php
$url = "http://asdsfsfsfsfsdfad.com";
$file = file_get_contents($url);

if(preg_match("/<title>(.+)<\/title>/i",$file,$m))
    print "$m[1]";
else
    print "The page doesn't have a title tag";
?>

当url是正确的url时,它可以正常工作,但是当我胡说八道时,我会收到两条警告消息:

Warning: file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Navn eller tjeneste ukendt in /var/www/web17/web/administration/custom_pages.php(71) : eval()'d code on line 4
Warning: file_get_contents(http://asdsfsfsfsfsdfad.com) [function.file-get-contents]: failed to open stream: php_network_getaddresses: getaddrinfo failed: Navn eller tjeneste ukendt in /var/www/web17/web/administration/custom_pages.php(71) : eval()'d code on line 4

有什么办法可以防止这种情况?

implode()期望第二个参数是一个数组,因此,在进行内爆之前,请检查$file是否为数组。

$file = is_array($file) ? implode("",$file) : $file;

甚至更好的是,使用file_get_contents ,则无需使用implode

$url = "http://asdsfsfsfsfsdfad.com";
$file = file_get_contents($url);

最简单的解决方案是仅抑制错误:

echo @file_get_contents("http://asdsfsfsfsfsdfad.com");

但是,错误抑制通常被认为是不好的做法,因为您永远不知道出了什么问题,因此最好有一个处理程序来选择性地处理错误,例如

set_error_handler(function($code, $message) {
    return ($code === E_WARNING && strpos($message, 'php_network_getaddresses'));
});
echo file_get_contents("http://asdsfsfsfsfsdfad.com");

这将禁止任何E_WARNINGS消息包含“ php_network_getaddresses”。 任何其他警告都不会被禁止。

另外,您不希望Regex解析HTML,而是使用HTML解析器,例如

因此,您可以使用DOM。 同样,使用错误抑制(错误)

$dom = new DOMDocument;
@$dom->loadHTMLFile("http://asdsfsfsfsfsdfad.com");
$titles = $dom->getElementsByTagName('title');
echo $titles->length ? $dom->nodeValue : 'No Title found';

或选择性地抑制网络错误:

set_error_handler(function($code, $message) {
    return ($code === E_WARNING && strpos($message, 'php_network_getaddresses'));
});

$dom = new DOMDocument;
$dom->loadHTMLFile("http://asdsfsfsfsfsdfad.com");
$titles = $dom->getElementsByTagName('title');
echo $titles->length ? $titles->item(0)->nodeValue  : 'No Title found';

但是,这将导致解析错误,因为loadHTMLFile不会返回任何HTML,因此也要抑制解析错误,您必须执行以下操作:

set_error_handler(function($code, $message) {
    return ($code === E_WARNING && strpos($message, 'php_network_getaddresses'));
});
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTMLFile("http://asdsfsfsfsfsdfad.com");
libxml_clear_errors();
$titles = $dom->getElementsByTagName('title');
echo $titles->length ? $titles->item(0)->nodeValue : 'No Title found';

在加入之前,您应该检查$file值是否为false:

$url = "http://asdsfsfsfsfsdfad.com";
$file = file($url);
if ($file !== false) {
    $file = implode("",$file);
    if(preg_match("/<title>(.+)<\/title>/i",$file,$m)) {
        print "$m[1]";
    } else {
      print "The page doesn't have a title tag";
    }
} else {
    print "wrong url";
}

您可以检查$ file是否为数组..

如果您检查它,它将永远不会给您错误。

if(is_array($file) && count($file)>0){
   if(preg_match("/<title>(.+)<\/title>/i",$file,$m))
     print "$m[1]";
   else
     print "The page doesn't have a title tag";

}
else{
   echo "$file is not arrya so it does not go in the fi block.";
}

您无需在文件内容字符串周围添加引号。 当您使用函数file_get_contents时,它已经以字符串形式返回结果。 通过在其周围加上双引号,基本上就不会在字符串中添加任何内容。

您可以使用curl来检查网址是否有效:

<?
function url_exists($strURL) {
    $resURL = curl_init();
    curl_setopt($resURL, CURLOPT_URL, $strURL);
    curl_setopt($resURL, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($resURL, CURLOPT_HEADERFUNCTION, 'curlHeaderCallback');
    curl_setopt($resURL, CURLOPT_FAILONERROR, 1);

    curl_exec ($resURL);

    $intReturnCode = curl_getinfo($resURL, CURLINFO_HTTP_CODE);
    curl_close ($resURL);

    if ($intReturnCode != 200 && $intReturnCode != 302 && $intReturnCode != 304) {
       return false;
    }Else{
        return true ;
    }
}

//Usage Example :
If(url_exists("http://www.weberdev.com/addexample.php3")) {
    Echo"URL Exists";
}Else{
    Echo"URL doesnot exist";
}
?>

有关更多信息,请参见http://www.weberdev.com/get_example.php3?ExampleID=4335

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM