如何从PHP中的HTTP头读取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;

但是,我得到了以下错误。

警告:stream_get_contents()要求参数1为资源,中给出的字符串

有人能告诉我如何从HTTP主体获取JSON消息吗?谢谢

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

更多信息