• Translate


Tutorial How to Send and Receive JSON Data with PHP cURL

Tutorial PHP |


Tutorial How to Send and Receive JSON Data with PHP cURL

When we work with web services and APIs, sending JSON data via POST request is the most needed function. PHP cURL makes it easy to send JSON data to URL. On this occasion, alltutorials will show you how to post JSON data using PHP cURL and get JSON data in PHP.

Send JSON data via POST with PHP cURL

Below is an example of making a HTTP POST request and sending JSON data to a URL with cURL in PHP.
  • Specify the URL ($url) where the JSON data needs to be sent
  • Start a new cURL resource by using the curl_init() function.
  • Arrange data inside Array PHP and encode into JSON string using json_encode() function.
  • Attach JSON data to the POST field using the CURLOPT_POSTFIELDS option.
  • Specify the request content type to the app/json using the CURLOPT_HTTPHEADER option.
  • Return the response as a string instead of having to issue it using the CURLOPT_RETURNTRANSFER option.
  • End by using the curl_exec() function to execute POST requests.

$url = 'http://www.example.com/api';

//create a new cURL resource
$ch = curl_init($url);

//setup request to send json via POST
$data = array(
    'username' => 'alltutorials',
    'password' => 'password123456'
$payload = json_encode(array("user" => $data));

//attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

//set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

//return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//execute the POST request
$result = curl_exec($ch);

//close cURL resource

//Output response
echo "<pre>$result</pre>";

//get response
$data = json_decode(file_get_contents('php://input'), true);

//output response
echo '<pre>'.$data.'</pre>';

Accept JSON POST data using PHP

The following example shows how you can get or retrieve JSON POST data using PHP.

JSON data will be translated using the json_encode() and file_get_contents() function to receive data in a more readable format.

Download Script

If you are interested to try the script above then can DOWNLOAD HERE

May be useful.

How to style text in Disqus comments Top Disqus Commentators
  • To write a bold letter please use <strong></strong> or <b></b>.
  • To write a italic letter please use <em></em> or <i></i>.
  • To write a underline letter please use <u></u>.
  • To write a strikethrought letter please use <strike></strike>.
  • To write HTML code, please use <code></code> or <pre></pre> or <pre><code></code></pre>, and please parse the code in the parser box below.
Show Parser Box

4 comments Add Comment

Nice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating.more 
php jobs in hyderabad.


Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here Instagram web viewer


I was searching for the stuff that is unique and speechless. I am come a cross to your site and found it according to my need and demand. instagram online


I think this happened because of technology is much cheaper. They should increase new technology instagram online


Comments not appropriate topic will sign SPAM.