In my bachelor thesis, I develop an interface between a Java and a PHP application. Hence, I have to transfer data between the two applications. I decided to use a POST request in order to transfer data from the Java to the PHP application. Since sending a POST request from Java is not documented that good yet, I would like to show you my working code:
try { // open a connection to the site URL url = new URL("http://www.yourdomain.com/yourphpscript.php"); URLConnection con = url.openConnection(); // activate the output con.setDoOutput(true); PrintStream ps = new PrintStream(con.getOutputStream()); // send your parameters to your site ps.print("firstKey=firstValue"); ps.print("&secondKey=secondValue"); // we have to get the input stream in order to actually send the request con.getInputStream(); // close the print stream ps.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
If you have to transfer more complex data like an array, I would suggest using JSON and use json_decode on the PHP side. You can check your JSON strings with a JSON validator.
If you do not know why you have to call con.getInputStream() in order to send the request, read the answer on StackOverflow.
In your PHP script, you can get the values like this:
foreach ($_POST as $key => $value) { switch ($key) { case 'firstKey': $firstKey = $value; break; case 'secondKey': $secondKey = $value; break; default: break; } }
Hope this helps someone 🙂
Unfortunately you haven’t written how to retrieve the POST response.
I foound this code:
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
Regards
Oliver
Very helpful, helped me with my new application that allows people to use a web interface or my Java interface. Thank you very much for this article.
i tried this to take a value from a text document and post it to a site but i do not understand what variable is the one that is being sent
In the example above, two key/value pairs are being sent: (firstKey, firstValue) and (secondKey, secondValue). You can see this in line 9 and 10 of the Java code.
how would i change that to send the value of a java variable?
Your variable has to be of type string. If you have complex data use something like JSON or some other form of serialisation. Then just use simple string concatenation to output your variable at the place where currently e.g. firstValue stands.
thanks for this helpful method but:
how to do the opposite. send data from php to java?
I have no tutorial for that. For my last application, I started a Java program from PHP via JNLP and was able to define command line params in the JNLP file.
I am not sure about the key value pair syntax in lines 9 and 10. Would I put
ps.print("firstKey=MYFIRSTVALUE");
? Also is the „&“ necessary for the second pair and all ensuing pairs after the second one?Yes, your code looks good. And yeah, all pairs after the first one have to be preceded by an ampersand „&“.
thank you, I’ve been trying to do this for a long time, couldn’t do shit.
Thanks for the code
Hello Simon,
I am a beginner at PHP code. I don’t understand how to make the PHP code prove it is actually working.
I have the JAVA code compiling and appears to be working, but how do I make the PHP code print something to a page to prove that the variables are being passed from JAVA to PHP?
1. I am sorry i just don’t know what to do. Can someone help with this problem?
2. Regarding Oliver’s respond. Is his code needed to make passing the variables from JAVA to PHP work? I don’t understand what Oliver is talking about.
Thank you for all your work here. I really need to get this to work as I am building a JAVA game that needs to pass information to a PHP script.
Thank you!!
Ken
Merci beaucoup ca m’as trop aidé cette article.