file_get_contents when url doesn't exist
You need to check the HTTP response code:
function get_http_response_code($url) {
$headers = get_headers($url);
return substr($headers[0], 9, 3);
}
if(get_http_response_code('http://somenotrealurl.com/notrealpage') != "200"){
echo "error";
}else{
file_get_contents('http://somenotrealurl.com/notrealpage');
}
file_get_contents returns 404 when URL is opened with the Browser and URL is valid
Sometimes a website will block crawlers(from remote servers) from getting to their pages.
What they do to work around this is spoof a browsers headers. Like pretend to be Mozilla Firefox instead of the sneaky PHP web scraper they are.
This is a function which uses the cURL library to do just that.
function get_data($url) {
$userAgent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13';
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html = curl_exec($ch);
if (!$html) {
echo "<br />cURL error number:" .curl_errno($ch);
echo "<br />cURL error:" . curl_error($ch);
exit;
}
else{
return $html;
}
//End of cURL function
}
One would then call it as below:
$response = get_data($requesturl);
Curl offers much more options in fetching of remote content and error checking than file_get_contents does. If you even want to customize it further, check out the list of cURL options here - Abridged list of cURL options
File_get_contents not evaluating to false when file does not exist
file_get_contents()
generates an E_WARNING level error (failed to open stream) which is what you'll want to suppress as you're already handling it with your exception class.
You can suppress this warning by adding PHP's error control operator @
in front of file_get_contents()
, example:
<?php
$path = 'test.php';
if (@file_get_contents($path) === false) {
echo 'false';
die();
}
echo 'true';
?>
The above echoes false, without the @
operator it returns both the E_WARNING and the echoed false. It may be the case that the warning error is interfering with your throw function, but without seeing the code for that it's hard to say.
file_get_contents false when url have spaces (encode everything not working)
use this function
function escapefile_url($url){
$parts = parse_url($url);
$path_parts = array_map('rawurldecode', explode('/', $parts['path']));
return
$parts['scheme'] . '://' .
$parts['host'] .
implode('/', array_map('rawurlencode', $path_parts))
;
}
echo escapefile_url("http://example.com/foo/bar bof/some file.jpg") . "\n";
echo escapefile_url("http://example.com/foo/bar+bof/some+file.jpg") . "\n";
echo escapefile_url("http://example.com/foo/bar%20bof/some%20file.jpg") . "\n";
How can I handle the warning of file_get_contents() function in PHP?
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);
Related Topics
PHP Method Argument Type Hinting with Question Mark (Type)
Datetime Class VS. Native PHP Date-Functions
What's Difference Between _Construct and Function with Same Name as Class Has
Parse Error: Syntax Error, Unexpected T_Function Line 10
MySQL Error When Inserting Data Containing Apostrophes (Single Quotes)
Laravel Livewire Component Not Refreshing/Reloading Automatically After Refreshing It
Jquery: Running Ajax Locally Without a Webserver
Pdo Prepared Statement Fetch() Returning Double Results
PHP on Godaddy Linux Shared Trying to Send Through Gmail Smtp
Pass Array Literal to Postgresql Function
PHP Files Are Downloaded by Browser Instead of Processed by Local Dev Server (Mamp)
Pdo SQLite Could Not Find Driver... PHP File Not Processing
Array to Object and Object to Array in PHP - Interesting Behaviour
How to Correct the [Composer\Downloader\Transportexception] Error for Composer