简体   繁体   English

通过POST将数据从Java发送到PHP

[英]Sending data from Java to PHP via POST

On one side I have a Java program that keeps feeding a database. 一方面,我有一个Java程序,它一直在为数据库提供数据。 On the other a web server (Apache). 另一个是Web服务器(Apache)。 I need to transfer multiple entries, at once, from the program to a distant web server via a REST webservice. 我需要通过REST Web服务一次将多个条目从程序传输到远程Web服务器。 While I have no trouble to send pairs of values from one to the others (using a HttpClient on the Java's side and a PHP file that writes into the server's own database on the other side), I lack knowledge about how I could build my HTTP request with data. 虽然我可以毫不费力地将一对值从一个发送到另一个(使用Java端的HttpClient和另一端写入服务器自己的数据库的PHP文件),但我不知道如何构建我的HTTP请求数据。

I've been using an Entity so far : 到目前为止,我一直在使用实体:

List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("field1", "aaa"));
list.add(new BasicNameValuePair("field2", "bbb"));

httppost.setEntity(new UrlEncodedFormEntity(list));

and receiving it in my PHP file : 并在我的PHP文件中接收它:

<?php
    $field1=$_POST["field1"];
    $field2=$_POST["field2"];
    $con= mysql_connect("localhost","root");
    if(!$con) die("Not able to connect");
    mysql_select_db("mydb",$con);
    mysql_query("INSERT INTO `mytable` (`field1`, `field2`) VALUES ('$field1', '$field2')");
    mysql_close($con);
?>

Works great. 效果很好。 Simple and easy. 简单易行。 But now, as stated before, I wish to transfer several entries from my database. 但是现在,如前所述,我希望从我的数据库中传输几个条目。 Each entry consists of 35 fields, which yields to some big data. 每个条目由35个字段组成,这些字段产生一些大数据。

My question is : How can I insert my database entries within ONE (or as many as needed if one HTTP request capacity is full) and fetch the resulting data on the PHP file side ? 我的问题是: 我如何在一个(或者如果一个HTTP请求容量已满)中插入我的数据库条目并在PHP文件端获取结果数据? Bonus : with the previous solution, how can I compress that data? 额外奖励:使用之前的解决方案,我该如何压缩这些数据?

You can use JSON for quick and simple transfer. 您可以使用JSON进行快速简单的传输。 For decoding, PHP supports $array = json_decode($json_string, TRUE); 对于解码,PHP支持$array = json_decode($json_string, TRUE); .

Not sure about compressing. 不确定压缩。 Are you going to transfer lots of data this way to make it relevant? 您是否会以这种方式传输大量数据以使其相关?

Well, the thing you are doing will lead to SQL injection easily. 好吧,你正在做的事情将导致SQL注入很容易。 Make sure you escape your data before running the insert query with mysql_real_escape_string or better - prepared statements. 在使用mysql_real_escape_string或更好的预处理语句运行插入查询之前,请确保转义数据。

Instead of using JSON, you could use Google Protocol Buffers . 您可以使用Google Protocol Buffers而不是使用JSON。 You need both JAVA and PHP extension to use protocol buffers but this is binary protocol that compresses the data amazingly well and allows you the flexibility to define different "repeatable" objects (entries) with same properties, so that when you open the protocol buffer on the PHP side, it will be easy to utilize the data. 您需要JAVA和PHP扩展来使用协议缓冲区,但这是二进制协议,可以非常好地压缩数据并允许您灵活地定义具有相同属性的不同“可重复”对象(条目),以便在打开协议缓冲区时在PHP方面,它将很容易利用数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM