Receive JSON Data With PHP CURL – Simple Example

Welcome to a tutorial on how to receive JSON data with PHP CURL. Are you expecting to receive some JSON data with a CURL call?

To receive JSON data with PHP CURL, simply use the json_decode() function to turn the JSON string back into an object or array:

  • $ch = curl_init();
  • curl_setopt($ch, CURLOPT_URL, "http://site.com");
  • curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  • $result = json_decode(curl_exec($ch));
  • curl_close($ch);

That covers the quick basics, but let us walk through a detailed example – Read on!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes CURL JSON Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

JSON DATA WITH PHP CURL

All right, let us now get into an example of receiving and decoding JSON data with PHP CURL.

 

DUMMY JSON DATA

dummy.php
<?php
$data = [
  "name" => "Jon Doe",
  "email" => "jon@doe.com",
  "colors" => ["Red", "Green", "Blue"]
];
echo json_encode($data);

Nothing to see here. Just a dummy script for testing, to output a dummy JSON encoded string.

 

CURL RECEIVE JSON DATA

fetch-json.php
<?php
// (A) CURL FETCH
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/dummy.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
 
// (B) RESULTS
// (B1) ERROR
if (curl_errno($ch)) { echo curl_error($ch); }
 
// (B2) JSON DECODE
else {
  echo $result; // STRING
  $decoded = json_decode($result);
  print_r($decoded); // STANDARD OBJECT
  $decoded = json_decode($result, true);
  print_r($decoded); // ARRAY
}
 
// (B3) CURL CLOSE
curl_close($ch);

This is pretty much the same as the snippet in the introduction, but take extra note of $result:

  • In the “raw” form, direct from the server – It is a JSON encoded string.
  • After we do a json_decode($result), it will be parsed into an object. Yes, this is a common pitfall among beginners – Not an array, but an object.
  • If you want to parse the results into an array, do json_decode($result, true) instead.

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

JSON Data With PHP CURL (click to enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *