如何从 PHP 中的 HTTP header 读取 JSON 消息

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

我正在像这样从一个 PHP 文件向另一个文件发送 JSON 请求。

$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
curl_close($curl);

我只是想让 JSON 请求发送到这个 2.php

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

但是,我收到以下错误。

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

谁能告诉我如何从 HTTP body 获取 JSON 消息?谢谢。

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

more info