Send JSON Data With PHP CURL (POST & GET)

Welcome to a tutorial on how to send JSON data with PHP CURL. Need to send some data using CURL in the JSON format?

To send JSON data with PHP CURL, simply json_encode() the data before sending it out:

  • $ch = curl_init();
  • curl_setopt($ch, CURLOPT_URL, "http://site.com");
  • curl_setopt($ch, CURLOPT_POST, true);
  • curl_setopt($ch, CURLOPT_POSTFIELDS, ["KEY" => json_encode(["VALUE", "VALUE"])]);
  • $result = curl_exec($ch);
  • curl_close($ch);

That covers the quick basics, but let us walk through more examples – 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.

 

 

SEND JSON DATA WITH PHP CURL

All right, let us now get into the examples of sending JSON data with PHP CURL.

 

CURL POST JSON DATA

1-post.php
<?php
// (A) CURL INIT
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/dummy.php");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// (B) SET DATA TO SEND
curl_setopt($ch, CURLOPT_POSTFIELDS, [
  "name" => "Jon Doe",
  "email" => "jon@doe.com",
  // NOTE - JSON ENCODE - ARRAY TO STRING
  "colors" => json_encode(["Red", "Green", "Blue"])
]);

// (C) CURL SEND
$result = curl_exec($ch);
if (curl_errno($ch)) { echo curl_error($ch); }
else { echo $result; }
curl_close($ch);

This should be pretty self-explanatory.

  • Use curl_setopt($ch, CURLOPT_POST, true) to specify there is POST data.
  • Then, append the data itself using curl_setopt($ch, CURLOPT_POSTFIELDS, [KEY => VALUE].
  • Lastly, json_encode() whichever field you need to send in JSON format.

 

 

CURL GET JSON DATA

2-get.php
<?php
// (A) DATA TO QUERY STRING
$data = [
  "name" => "Jon Doe",
  "email" => "jon@doe.com",
  // TAKE NOTE - JSON ENCODED COLORS
  "colors" => json_encode(["Red", "Green", "Blue"])
];
$data = "?" . http_build_query($data);
 
// (B) CURL INIT
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/dummy.php" . $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
// (C) CURL SEND
$result = curl_exec($ch);
if (curl_errno($ch)) { echo curl_error($ch); }
else { echo $result; }
curl_close($ch);

How about using GET to send data then? This should not be a surprise – Instead of setting CURLOPT_POST, we directly encode the data array into a query string and append it to the URL.

 

 

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

Send 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 *