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:
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 u开发者_高级运维kendt in /var/www/web17/web/administration/custom_pages.php(71) : eval()'d code on line 4
Any way to prevent this?
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'. 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
- How do you parse and process HTML/XML in PHP?
So you could do it with 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:
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()
expects the second parmeter to be an array, thus, check if $file
is an array before doing an implode.
$file = is_array($file) ? implode("",$file) : $file;
Or even better, use file_get_contents
, then you won't need to use implode
:
$url = "http://asdsfsfsfsfsdfad.com";
$file = file_get_contents($url);
You should check the $file
value for false before joining:
$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 ..
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. 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:
<?
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.
精彩评论