I am trying to develop small API code in php, which will send POST request in json format using PHP curl (from client side). And at receiving side(at server side) I want to parse the request and process it. but at receiving side it gives me empty array in post field. Below is my code
Client side:
<?php
$data = array("name" => "Hagrid", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, "xdata=".$data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
echo $result = curl_exec($ch);
?>
At server side:
<?php
print_r($_POST);
?>
Blockquote Array ( ) Blockquote
It always gives me empty POST array at server side. Am I doing things wrong or is there another way to parse the request? Please guide me.