我有这样的代码:
<?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时,它可以正常工作,但是当我输入无意义的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
有什么办法可以防止这种情况发生吗?
最简单的解决方案就是直接抑制错误:
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");
这将抑制任何带有'php_network_getaddresses'消息的E_WARNINGS。任何其他警告将不会被抑制。
此外,您不希望Regex解析HTML,而是使用HTML解析器,如
中给出的其中一个- 如何在PHP中解析和处理HTML/XML ?
你可以用DOM来做。同样,使用错误抑制(bad)
$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';
implode()
期望第二个参数为数组,因此,在进行内爆之前检查$file
是否为数组。
$file = is_array($file) ? implode("",$file) : $file;
或者更好,使用file_get_contents
,那么你就不需要使用implode
:
$url = "http://asdsfsfsfsfsdfad.com";
$file = file_get_contents($url);
您应该在加入之前检查$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是否为array .
如果你检查它,它永远不会给你一个错误…
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来检查url是否有效:
<?
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";
}
?>