Link to home
Start Free TrialLog in
Avatar of GVNPublic123
GVNPublic123

asked on

file_get_contents

In PHP using function file_get_contents(), is it possible to hide when it outputs warning or error (for instance when http request is denied it would display warning, is there any way to get rid of that?

Maybe with try, catch?
ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GVNPublic123
GVNPublic123

ASKER

Well, as this is only 1 time occurrence requirement, @ will probably do the job. Thanks!
Assuming something like this:
$site="http://www.google.com";
$content = file_get_content($site);
echo $content;

Step 1: check the return code: if ($content==FALSE) { handle error here... }
Step 2: suppress the warning by putting an @ in front of the file_get_content: $content = @file_get_content($site); (note, use this sparingly!! See Error Control Operators)

or

$content = @file_get_contents("http://www.google.com");
if (strpos($http_response_header[0], "200")) {
   echo "SUCCESS";
} else {
   echo "FAILED";
}