[英]Passing String array to PHP as POST
我試圖將一個字符串數組作為POST數據傳遞給PHP腳本但不確定該怎么做。
這是我到目前為止執行PHP腳本的代碼:
我試圖傳遞數組的地方:
nameValuePairs.add(new BasicNameValuePair("message",message));
String [] devices = {device1,device2,device3};
nameValuePairs.add(new BasicNameValuePair("devices", devices));// <-- Can't pass String[] to BasicNameValuePair
callPHPScript("notify_devices", nameValuePairs);
調用PHP腳本:
public String callPHPScript(String scriptName, List<NameValuePair> parameters) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://localhost/" + scriptName);
String line = "";
StringBuilder stringBuilder = new StringBuilder();
try {
post.setEntity(new UrlEncodedFormEntity(parameters));
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() != 200)
{
System.out.println("DB: Error executing script !");
}
else {
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
line = "";
while ((line = rd.readLine()) != null) {
stringBuilder.append(line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("DB: Result: " + stringBuilder.toString());
return stringBuilder.toString();
}
和PHP腳本有問題:
<?php
include('tools.php');
// Replace with real BROWSER API key from Google APIs
$apiKey = "123456";
// Replace with real client registration IDs
$registrationIDs = array($_POST[devices]); <-- Where I want to pass array to script
// Message to be sent
$message = $_POST['message'];
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
print_as_json($result);
?>
有任何想法嗎? 謝謝 !
編輯
我正在嘗試以下但仍然沒有快樂:
public void notifyDevices(Message message) {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
List<String> deviceIDsList = new ArrayList<String>();
String [] deviceIDArray;
//Get devices to notify
List<JSONDeviceProfile> deviceList = getDevicesToNotify();
for(JSONDeviceProfile device : deviceList) {
deviceIDsList.add(device.getDeviceId());
}
//Array of device IDs
deviceIDArray = deviceIDsList.toArray(new String[deviceIDsList.size()]);
for(String deviceID : deviceIDArray) {
nameValuePairs.add(new BasicNameValuePair("devices[]", deviceID));
}
//Call script
callPHPScript("GCM.php", nameValuePairs);
}
這就是我所有的“錯誤報告”......
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() != 200)
{
System.out.println("DB: Error executing script !");
}
要在查詢字符串中將數組傳遞給php,您應該將[]
添加到標識符並將每個項目添加為單獨的條目,因此這樣的事情應該起作用:
nameValuePairs.add(new BasicNameValuePair("devices[]", device1));
nameValuePairs.add(new BasicNameValuePair("devices[]", device2));
nameValuePairs.add(new BasicNameValuePair("devices[]", device3));
現在,PHP端的$_POST['devices']
將包含一個數組。
我認為你應該對你的設備數組進行json編碼,這樣你就可以獲得一個字符串,你可以將它傳遞給BasicNameValuePair(...)。 在你的PHP代碼中,你只需要使用json_decode來獲取一個數組。
JSONArray devices = new JSONArray();
devices.put(device1);
devices.put(device2);
devices.put(device3);
String json = devices.toString();
nameValuePairs.add(new BasicNameValuePair("devices", devices));
在你的PHP代碼中:
$devices = $_POST['devices'];
$devices = json_decode($devices);
首先,在PHP中訪問$_POST
數組時,您缺少單引號。 改變線
$registrationIDs = array($_POST[devices]);
至:
$registrationIDs = array($_POST['devices']);
您應該使用ini值display_errors
, log_errors
, error_reporting
啟用錯誤日志記錄或PHP錯誤消息的輸出以進行調試,以了解此類錯誤。
但即使是array($_POST['devices'])
也不會做出可能的預期。 array(...)
是php中的數組初始化構造。 這意味着你只需將($ _POST ['devices'])包裝到另一個數組中。
...想看var_dump($_POST);
的輸出var_dump($_POST);
。 這會讓我有機會進一步幫助..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.