Hi! Here let's see how to post json data with php curl and receive the raw json sent. You can simply use PHP file_get_contents() to send HTTP GET request. But it's not the same case with HTTP POST. Fortunately PHP cURL makes the process very simple. With curl you can easily send HTTP requests. In case you don't know, 'cURL' is a library that let you communicate with web servers using different protocols such as http, https, ftp, telnet, file etc.
To use curl, you must install libcurl package on your server according to the version of PHP you use.
In case you are not sure if curl is enabled on your machine or not, use this code to check it.
<?php echo (is_callable('curl_init') ? 'cURL is enabled' : 'cURL is disabled'); ?>
How to Post JSON with PHP cURL?
In order to send HTTP POST request with JSON data, you have to follow some steps and they are,
- Format JSON data correctly
- Attach JSON to the body of the POST request
- Set the right content type with HTTP headers
- Send the request.
The following is the PHP code to send json through POST request.
<?php
// url
$url = 'http://mydomain.com/api/users/create';
// json data
$data = array(
'site' => 'mydomain.com',
'account' => 'admin',
'status' => 'true'
);
$json = json_encode($data);
// send post request
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
Here is the explanation for the functions & curl options we have used in the above script:
- curl_init() - Initialize a curl session, takes up a parameter URL to send post request.
- curl_setopt() - Set the curl options.
- CURLOPT_POSTFIELDS - This option will set the data to the body of the http request.
- CURLOPT_HTTPHEADER - Option to set http headers. It is important to define the right content type here, so that the remote server understands what we sent. To send json, the content type should be set to 'application/json'.
- CURLOPT_RETURNTRANSFER - Setting this to TRUE will place the response from the remote server on $result variable instead of outputting it.
- curl_exec() - Send the HTTP request.
- curl_close() - Close the curl session.
Retrieve the Posted JSON:
Okay! We have sent json data via http post request. But what will happen at the receiving end? How should we retrieve the incoming json?
Well! In our example we have sent raw json data. This can be accessed through php's input stream. Below is the code to receive the json posted,
<?php
$output = file_get_contents("php://input");
echo $output;
?>
You can decode the json and process it as you want. Pretty simple!
Read Also:That explains about posting json data and receiving it using PHP cURL. This is not very different from sending a regular POST request. However, we are doing it over HTTP here. Hopefully, this tutorial is useful for you. Please don't forget to share it with your friends.