Sending JSON Post Request To Server In PHP

Hey Mates,

This is Amit Chauhan

I recently got stuck in a situation where in order to access the API I had to send the post request in JSON format to the server I searched for hours got many hacks but unfortunately, none worked but finally, I got success when I used CURL library to send the JSON request to the server.

Today I will be going to share the hack of my problem with you all so that no other mate finds it difficult like I did.

In order to send the JSON data to the remote server, we will use CURL library. Remember CURL library is automatically installed in the Apache we just need to uncomment it in our php.ini file. Just go to

Xampp->PHP->php.ini  and search for  ;extension=php_curl.dll  and uncomment the line by removing ; so now it will look like extension=php_curl.dll

Now restart your apache for the changes to take place.

Now we are all set to go.

Let us suppose we have a situation where we have to get the list of all flights which are available for the particular destination on a particular day with particular no. of passengers. The data can only be accessed by sending a post request to the server in JSON format which in turn will return us the list of all flights with fare etc.

So let’s start

Now first let us specify the URL where data needs to send


The second step is to construct your data which later will be encoded into JSON format.

For eg

$jsonData = array(

‘authentication’ => array(‘search’ => array(‘adults’=>’2′,’children’=>’0′,’infants’=>’0′,’cabin’=>’*’,’flights’=>array([‘from’=>’DEL’,’to’=>’MUM’,’date’=>2017-09-30]))



Now we will encode the data into JSON format

$data_json = json_encode($jsonData);


Now we will use CURL to send the request

$ch = curl_init();  [Initalise Curl Library]

curl_setopt($ch, CURLOPT_URL, $url);                      //Set the URL of server or API

curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: application/json’));          //Specify the header  type (type of data you will receive from server)

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);             //set the data to be send to the set

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response  = curl_exec($ch);  //Execute the CURL request

curl_close($ch);       //Close the curl  connection



Now we have received the data in JSON format in $response variable in order to access the json data we need to convert them either into object or array.

In this case, we will decode the JSON data into an Array format. Remember sending true as the second parameter will tell the server to decode the JSON into an array format by default it’s in object format.


$yummy = json_decode($response,true);


Now depending on data iterate or access the data you aspired For eg.


yummy[‘response’];  //This will print the response here response is an array index.


So Guys this the way we can send the JSON data to the server by encoding it and get the response from the server and decoding it into an object or array format.

For getting above code Download Here

You can also view some of our trending topics such as

So Guys stay tuned for our next tutorial till then keep developing smart things.

You can also visit our Write to Us section and can share your tutorials with us whose hacks you have earned with your smartness and handwork.

So mates keep rocking Because we believe Hurdles aren’t really Hurdles.


Be First to Comment

Leave a Reply

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