简体   繁体   English

当file_get_content错误的URL时发出警告

[英]Warnings when file_get_content wrong url

I have this code: 我有以下代码:

<?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";
?>

It works fine when the url is a proper url, but when I put in nonsense then I get two warning messages: 当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

Any way to prevent this? 有什么办法可以防止这种情况?

implode() expects the second parmeter to be an array, thus, check if $file is an array before doing an implode. implode()期望第二个参数是一个数组,因此,在进行内爆之前,请检查$file是否为数组。

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

Or even better, use file_get_contents , then you won't need to use implode : 甚至更好的是,使用file_get_contents ,则无需使用implode

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

The easiest solution would be to just suppress the error: 最简单的解决方案是仅抑制错误:

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

However, error suppression is generally considered bad practise because you never know what went wrong, so it is better to have a handler that selectively handles errors, for instance 但是,错误抑制通常被认为是不好的做法,因为您永远不知道出了什么问题,因此最好有一个处理程序来选择性地处理错误,例如

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

This would suppress any E_WARNINGS with a message containing 'php_network_getaddresses'. 这将禁止任何E_WARNINGS消息包含“ php_network_getaddresses”。 Any other Warnings will not be suppressed. 任何其他警告都不会被禁止。

In addition, you dont want Regex to parse HTML, but use an HTML Parser, like one of those given in 另外,您不希望Regex解析HTML,而是使用HTML解析器,例如

So you could do it with DOM. 因此,您可以使用DOM。 Again, either using Error Suppression (bad) 同样,使用错误抑制(错误)

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

Or selectively suppressing network errors: 或选择性地抑制网络错误:

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';

However, this will then result in parsing errors because loadHTMLFile will not return any HTML, so to suppress the parsing errors as well, you'd have to do: 但是,这将导致解析错误,因为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';

You should check the $file value for false before joining: 在加入之前,您应该检查$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";
}

you can check whether $file is array or not .. 您可以检查$ file是否为数组..

if you check it then it will never give you an error.. 如果您检查它,它将永远不会给您错误。

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.";
}

You don't need to add the quotes around the file contents string. 您无需在文件内容字符串周围添加引号。 When you use the function file_get_contents, it already returns the results as a string. 当您使用函数file_get_contents时,它已经以字符串形式返回结果。 By adding those double quotes around it, you are basically adding nothing to the string. 通过在其周围加上双引号,基本上就不会在字符串中添加任何内容。

You can use curl to check if the url is valid: 您可以使用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";
}
?>

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

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

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