user3041764 user3041764 - 1 year ago 76
PHP Question

Send array as json and read that data

I am encoding array as json and sending it by curl to other file:

$data = array(
"authorizedKey" => "abbad35c5c01-xxxx-xxx",
"senderEmail" => "",
"recipientEmail" => "",
"comment" => "Invitation",
"forceDebitCard" => "false"

$data = json_encode($data);

$ch = curl_init('http://localhost/curl/1.php');
$headers = array('Accept: application/json','Content-Type: application/json');

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

echo curl_exec($ch);

Problem is http://localhost/curl/1.php dont recieve any
and I dont know how to operate on posted data.

http://localhost/curl/1.php output:

Host = localhost
Accept = application/json
Content-Type = application/json
Content-Length = 166

Answer Source

You are sending data by using request body, and you are supposed to get data by request body not $_POST super global. You can use following code to get whole data.

$entityBody = file_get_contents('php://input');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download