PHP – Suppress and catch file_get_contents() warning

If you come across the following error when using file_get_contents().

  • Warning: file_get_contents(www.example.com) [function.file-get-contents]: failed to open stream:

 

One simple solution is to suppress the warning by adding @ in front of the file_get_contents() and check its returned value.

$data = @file_get_contents('http://www.example.com');
if ($data) {
  // success
} else {
  // fail
}

 

You can find more workarounds in the reference link below.

Done =)

Reference: StackOverflow – how can I handle the warning of file_get_contents() function in php?

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.