2

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 ukendt in /var/www/web17/web/administration/custom_pages.php(71) : eval()'d code on line 4

Any way to prevent this?

Brian
  • 355
  • 1
  • 5
  • 13
  • 1
    What kind of warnings are you getting? Please update your original post with more information about the issue you're facing. – karllindmark Jul 26 '11 at 08:38
  • 2
    Side note: `$file = file($url); $file = implode("",$file);` is redundant. Use `$file = file_get_contents($url);` – Yoshi Jul 26 '11 at 08:39
  • 1
    Offtopic. You really should check out DOM extension http://php.net/dom – Anpher Jul 26 '11 at 08:45
  • @Brian Have you actually read what has been written here, before updating your question?? – Yoshi Jul 26 '11 at 08:50
  • Yes Yoshi. I tried replacing the implode with file_get_contens and I still got warning messages. Likewise when I tried the "Check if false" method. – Brian Jul 26 '11 at 08:53
  • @Brian just look at at @Shef's answer, to see *how* you should have used `file_get_contents`. – Yoshi Jul 26 '11 at 08:55
  • My bad, I can't quite figure out how to use stackoverflow's text editor. I had used the correct code, but just not pasted it correctly into my post. I still get the warnings. I have used @ to suppress them, but not really sure that's the correct way to go about it. – Brian Jul 26 '11 at 09:21

6 Answers6

1

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);
Shef
  • 44,808
  • 15
  • 79
  • 90
1

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

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';
Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
0

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";
}
Eineki
  • 14,773
  • 6
  • 50
  • 59
  • But I get the warnings even before checking at "$url = "http://asdsfsfsfsfsdfad.com"; $file = file($url); " – Brian Jul 26 '11 at 09:05
0

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.";
}
Rukmi Patel
  • 2,619
  • 9
  • 29
  • 41
0

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.

ShoeLace1291
  • 4,551
  • 12
  • 45
  • 81
  • Or you could use this to check to see if the file actually exists. $url = "http://asdsfsfsfsfsdfad.com"; if(file_exists($url)){ $file = file_get_contents($url); $file = implode("",$file); if(preg_match("/(.+)<\/title>/i",$file,$m)) print "$m[1]"; else print "The page doesn't have a title tag"; enter code here } – ShoeLace1291 Jul 26 '11 at 09:02
-2

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.

Tobias
  • 7,238
  • 10
  • 46
  • 77