将字符串数组作为POST传递给PHP [英] Passing String array to PHP as POST
问题描述
我试图将一个字符串数组作为POST数据传递给PHP脚本,但不知道该怎么做。
这是执行PHP脚本的代码远:
在哪里试图传递数组:
nameValuePairs.add(new BasicNameValuePair(message,message));
String [] devices = {device1,device2,device3};
nameValuePairs.add(新BasicNameValuePair( 装置,装置)); //< - 不能传递字符串[]至BasicNameValuePair
callPHPScript( notify_devices,namevaluepairs中); b
$ b 调用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();
尝试{
post.setEntity(new UrlEncodedFormEntity(parameters));
HttpResponse response = client.execute(post);
if(response.getStatusLine()。getStatusCode()!= 200)
{
System.out.println(DB:Error executed error!);
}
else {
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity()。getContent()));
line =; ((line = rd.readLine())!= null){
stringBuilder.append(line);
while
}
}
} catch(IOException e){
e.printStackTrace();
}
System.out.println(DB:Result:+ stringBuilder.toString());
return stringBuilder.toString();
}
以及PHP脚本:
<?php
include('tools.php');
//替换为Google API中真正的BROWSER API密钥
$ apiKey =123456;
//替换为真正的客户端注册ID
$ registrationIDs = array($ _ POST [devices]); < - 我想将数组传递给脚本
//要发送的消息
$ message = $ _POST ['message'];
//设置POST变量
$ 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'
);
//打开连接
$ ch = curl_init();
//设置网址,POST变量的数量,POST数据
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));
//执行后
$结果= curl_exec($ ch);
//关闭连接
curl_close($ ch);
print_as_json($ result);
?>
有什么想法?感谢!
编辑
public void notifyDevices(消息消息){
List< NameValuePair> nameValuePairs = new ArrayList< NameValuePair>();
列表< String> deviceIDsList = new ArrayList< String>();
String [] deviceIDArray;
//获取设备以通知
List< JSONDeviceProfile> deviceList = getDevicesToNotify(); (JSONDeviceProfile device:deviceList){
deviceIDsList.add(device.getDeviceId());
}
//设备ID数组
deviceIDArray = deviceIDsList.toArray(new String [deviceIDsList.size()]);
for(String deviceID:deviceIDArray){
nameValuePairs.add(new BasicNameValuePair(devices [],deviceID));
$ b $ //调用脚本
callPHPScript(GCM.php,nameValuePairs);
}
这是所有错误报告我有...
HttpResponse response = client.execute(post);
if(response.getStatusLine()。getStatusCode()!= 200)
{
System.out.println(DB:Error executed error!);
在查询字符串中,您应该为标识符添加 []
,并将每个项目添加为单独的条目,所以类似这样的内容应该可以工作:
nameValuePairs.add(new BasicNameValuePair(devices [],device1));
nameValuePairs.add(new BasicNameValuePair(devices [],device2));
nameValuePairs.add(new BasicNameValuePair(devices [],device3));
现在, $ _ POST ['devices']
在PHP方面将包含一个数组。
I am trying to pass a string array to a PHP script as POST data but am unsure of what to do.
Here is my code for executing PHP scripts so far:
Where I am trying to pass the array:
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);
Call PHP script:
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();
}
And the PHP script in question:
<?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);
?>
Any ideas? Thanks !
Edit
I am trying the following but still no joy:
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);
}
This is all the "Error reporting" I have...
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() != 200)
{
System.out.println("DB: Error executing script !");
}
To pass an array to php in query string, you should add []
to identifier and add every item as separate entry, so something like this should work:
nameValuePairs.add(new BasicNameValuePair("devices[]", device1));
nameValuePairs.add(new BasicNameValuePair("devices[]", device2));
nameValuePairs.add(new BasicNameValuePair("devices[]", device3));
now, $_POST['devices']
on php side will contain an array.
这篇关于将字符串数组作为POST传递给PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!