PHP - 接收Json(POST)

时间:2016-12-31 15:03:11

标签: php android json

我正在向服务器发送一个Json数组:

{
"Name":"Number",
"Name":"Number" 
, ...
}

我如何在PHP上收到它?

发送方法是POST。

这是我的android代码:

        HttpClient httpclient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost("http://192.168.1.7/admina/contacts_inout.php");

        String json = "";

        json = jsons.toString();

        StringEntity se = new StringEntity(json);

        httpPost.setEntity(se);

        httpPost.setHeader("Accept", "application/json");

        httpPost.setHeader("Content-type", "application/json");

        HttpResponse httpResponse = httpclient.execute(httpPost);

2 个答案:

答案 0 :(得分:1)

isset($_POST['your_param_key']) or exit('param not found.');

$json = $_POST['your_param_key'];

$array = json_decode($json, true);

print_r($array);

答案 1 :(得分:1)

由于PHP只处理POST数据,即application / x-www-form-urlencoded或multipart / form-data格式化,你必须使用RAW输入

使用

$sent_array = json_decode(file_get_contents('php://input'),true);

将数据作为数组