2016-04-16 22 views
-1

我正在從一個PHP文件發送JSON請求到另一個這樣的。如何從PHP的HTTP頭中讀取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); 

我只是試圖讓送到這個2.PHP

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

JSON的請求,但是,我得到下面的錯誤。

警告:stream_get_contents()預計參數1是資源,字符串中

給出

誰能告訴我怎麼去從HTTP體的JSON消息?謝謝。

回答

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

more info

相關問題