2014-03-05 68 views
0

我正在嘗試使用curl發送JSON數據,但它沒有發佈。如何使用curl發佈JSON數據?

$data = array("name" => " Hagrid", "age" => "36"); 
$content= json_encode($data,true); 
$url ='http://www.example.com/webervices/msg.php'; 
$curl = curl_init($url); 
curl_setopt($curl, CURLOPT_HEADER, false); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_HTTPHEADER, 
    array("Content-type: application/json")); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $content); 
$json_response = curl_exec($curl); 
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
print_r($status); 
curl_close($curl); 
$response = json_decode($json_response, true);  

而且在msg.php

print_r($_POST); 

但它打印空白陣列。

如果有人有任何使用php發佈JSON數據的解決方案,這將是有幫助的。

+0

你看着辦吧如何做到這一點了嗎?我來到現在像你一樣的問題= http://stackoverflow.com/questions/28309035/post-json-data-using-curl-php。我想知道是否可以回答 - 如果你知道答案。謝謝隊友:) – 2015-02-03 21:26:08

回答

0

在你msg.php腳本,你需要做到這一點,以獲得JSON內容:

$body = @file_get_contents('php://input'); 
print $body; 
+0

其工作的所有數據,但它不工作時發送圖像文件,$ body = @file_get_contents('php:// input'); print $ body;它賦予空白值。我應該如何使用json和curl發送圖像文件。 –