Sending Simple Data From Java to PHP via a POST Request

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 🙂

13 Gedanken zu „Sending Simple Data From Java to PHP via a POST Request“

  1. 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

  2. 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

    1. 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.

  3. 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?

  4. 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

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert