[英]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.