Consuming REST APIs with PHP and CURL

I wasted such a lot of time on this that I must commit it to the internet on the off chance that it helps someone else in the same situation.

If you are using PHP to consume a RESTful API via CURL and you want to manipulate the data you get back it’s very important that you set CURLOPT_RETURNTRANSFER to true. This allows you to collect the response from the server in a variable. If you don’t set this option it will just echo the return to the screen, which is obviously of no use whatsoever.

While I’m here I may as well mention as well that if you want json_decode to return an array you need to use json_decode($result, true); otherwise you get an object back. The final code I wrote looks like this:

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "http://YOUR_URL_HERE");
$result = curl_exec($ch);


$result = json_decode($result, true); // giving true to json_decode returns array

One thought on “Consuming REST APIs with PHP and CURL”

  1. Thanks, below code is cleaner and shorter

    $url = “http://localhost/php_rest_myblog/api/post/read.php”;
    $url_json = file_get_contents($url);
    $result = json_decode($url_json, true);

Leave a Reply

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