2016-12-31 528 views
0

我送一个JSON数组服务器:PHP - 接收JSON(POST)

{ 
"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); 
+1

您可以使用[php:// input](http://php.net/manual/en/wrappers.php.php#wrappers.php.input)阅读原始文章(因为您没有发布表单字段) )。一个简单的使用/例子是'if(getenv('REQUEST_METHOD')=='POST'){$ data = file_get_contents(“php:// input”); }' –

+0

@KraangPrime你的意思是:file_get_contents(“php:// input”); ? – Ghorbanzadeh

+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

由于PHP只处理后的数据是应用程序/ x-WWW的形式,进行了urlencoded或多/ form-data的格式,你将有使用RAW输入

使用

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

来获取数据作为数组