2016-12-31 108 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); 

來獲取數據作爲數組