I wrote a PHP code like this
$site="http://www.google.com";
$content = file_get_content($site);
echo $content;
But when I remove "http://" from $site
I get the following warning:
Warning: file_get_contents(www.google.com) [function.file-get-contents]: failed to open stream:
I tried try
and catch
but it didn't work.
Here's how I handle that:
You can prepend an @:
$content = @file_get_contents($site);
This will supress any warning - use sparingly!. See Error Control Operators
Edit: When you remove the 'http://' you're no longer looking for a web page, but a file on your disk called "www.google....."
Since PHP 4 use error_reporting():
Step 1: check the return code:
if($content === FALSE) { // handle error here... }
Step 2: suppress the warning by putting an error control operator (i.e.
@
) in front of the call to file_get_contents():$content = @file_get_contents($site);