Never use file_get_contents to make URL queries
Why you should not ever use file_get_contents
Page content
As each programmer by nature, I’m lazy. I try to accomplish as much as possible with minimum effort.
So whenever I use PHP to fetch something, I use file_get_contents
to make queries and pull data from other services. So basically, my favourite snippet is this guy here:
Problem
It’s not to bad - the only issue is error handling, if something goes wrong - it’s really hard to know what is happening. Obviously you can do something like this:
$result = file_get_contents("http://example.com");
if ($result === false) {
var_dump($http_response_header);
}
but it’s pretty shitty way to handle errors, and then look into array for details.
Solution
So what is the solution ? It’s quite simple use CURL
library instead:
What’s the difference ? You are getting much detailed errors and you can easily take actions based on exact error.