PHP - Consuming REST APIs with cURL or file_get_contents- 3 mins
Recently I was using PHP to consume some REST APIs with PHP. Normally I had done this through cURL, but during a code review my reviewer mentioned that I should have just used PHP’s file_get_contents(). I had seen a discussion on file_get_contents() vs cURL and decided that since cURL was more adaptable of a tool, it was the better choice. Turns out I was wrong. Metaphorically speaking, file_get_contents() is the trusty flathead screwdriver that gets the job done smooth and easy. I was using a power drill with multiple speed settings, a level, gel grip, and a sweet orange paintjob in order to tighten a loose leg on my chair.
Taking from haxx.se:
Client URL Library (cURL) allows you to transfer data from or to a server using one of the many different networking protocols:
The list goes on. The great thing about cURL is the customizability. cURL allows you to specify proxy support, authentication, HTTP post, SSL connections, cookies, etc. Basically, it has a ton of options that let you specify how you interact with the URL in so many useful ways, yet also provides you some pretty decent defaults.
Say I wanted to get some data from the google maps API using PHP, I could use the following code:
$url = "https://maps.googleapis.com/maps/api/geocode/json"; $data = array( "address" => "Oxford University, uk", "sensor" => "false" ); $query_url = sprintf("%s?%s", $url, http_build_query($data)); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $query_url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 10); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); $http_code = curl_getinfo($curl , CURLINFO_HTTP_CODE); $result = curl_exec($curl); header('Content-type: application/json'); echo $result; curl_close($curl);
Pretty beasty, right? I made sure that I didn’t care about verifying the SSL layer, set my timeout for the http request, made sure to set the returntransfer so I get the data back as a string…
But there’s a quicker way.
As long as I’ve got the proper configs (allow_url_fopen) set on my php.ini file, I can use file_get_contents in order to do the same thing in a very simple manner.
$url = "https://maps.googleapis.com/maps/api/geocode/json"; $data = array( "address" => "Oxford University, uk", "sensor" => "false" ); $query_url = sprintf("%s?%s", $url, http_build_query($data)); header('Content-type: application/json'); echo file_get_contents($query_url);
We still get our Google maps API data, exactly the same as before!
This would’ve saved me some headache in setting the cURL flags, and is just flat-out easier.
How do I know which one to use?
So clearly, file_get_contents is easier to use when you’re consuming APIs. You can even set a stream context in order to send POST/UPDATE requests with file_get_contents.
You might consider using cURL if:
- You need a very finely tuned request. See cURL options
- You don’t have allow_url_fopen allowed on your system.
- You need multiple parallel handlers
- You want to handle timeout settings (I guess these all go with the finely tuned request theme)
Basically, if you’re doing simple GET/POST requests and file_get_contents() is working for you, you’re fine. If you notice for some reason that your file_get_contents() is a little buggy and maybe you need to be a bit more specific with your request, opt for the cURL option.