Sree Sree - 10 months ago 59
PHP Question

How to read a JSON message from the HTTP header in PHP

I am sending a JSON request from one PHP file to another like this.

$curl = curl_init();

curl_setopt($curl,CURLOPT_URL, "http://localhost/2.php");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl,CURLOPT_POSTFIELDS, $req_json);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($req_json))

//execute post
$res_json = curl_exec($curl);

//close connection

I am just trying to get the JSON request sent to this 2.php

$req_json = stream_get_contents("php://input");
echo $req_json;

But, I am getting the below error.

Warning: stream_get_contents() expects parameter 1 to be resource, string given in

Can anyone tell me how to get the JSON message from the HTTP body? Thanks.

Sk_ Sk_
$req_json = file_get_contents('php://input');
echo $req_json;

more info