Sonntag, 11. November 2018

PHP Warning file_get_contents() best solution

PHP Warning:  file_get_contents(https://example.com/): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /var/www/virtual/blabla/test.php on line 123

There are many solutions to the problem posted on the Internet. Not all of these solutions really solve the problem. The challenge with this problem is the use of boolean (false / true).
$file_get_contents_url = "";
if (false !== ($content = @file_get_contents($file_get_contents_url))) {
// use here the filled variable $content for your code.
} elseif ($check === false) {
// here your code for "false".
} else {
// here your possible "fallback" code.
}
No guarantee. No warranty. No support.
Use at your own risk.