0

So I'm sending data to a PHP API from Java using POST.

What I'm trying to do is send a particular variable to the API in the POST request, and then use the value of it in my PHP. But currently in the PHP, $_POST["variable1"] is empty.

The script is definitely being called, as I'm sent an email each time it is, but the PHP isn't reading the POST variable.

My Java looks like this:

String line;
StringBuffer jsonString = new StringBuffer();

try {
    URL url = new URL("https://www.x.com/api.php");
    String payload = "{\"variable1\":\"value1\",\"variable2\":\"value2\"}";

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Accept", "application/json");
    connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
    OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
    writer.write(payload);
    writer.close();

    BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    while ((line = br.readLine()) != null) {
        jsonString.append(line);
    }
    br.close();
    connection.disconnect();
}

This is based on: How to send Request payload to REST API in java?

And the PHP API contains this:

if (isset($_POST["variable1"]) && isset($_POST["variable2"])) {
    //Send email containing POST values
    //Send 200 success header
} else {
    //Send error email
    //Send 400 error header
}

Currently I get the error email each time I run the Java code. So it is sending to the API, but the value isn't being read correctly.

Am I sending it correctly in Java? Or am I missing something in the PHP? Do I have to do something to decode it?

Thanks for any help. I've been working on this for a while and just can't seem to work out what the issue is.

Community
  • 1
  • 1
user3420034
  • 1,275
  • 2
  • 20
  • 43

2 Answers2

2

The $_POST variable is not set for all HTTP POST requests, but only for specific types, e.g application/x-www-form-urlencoded.

Since you are posting a request containing JSON entity (application/json), you need to access it as follows.

$json = file_get_contents('php://input');
$entity= json_decode($json, TRUE);
pgiecek
  • 7,508
  • 3
  • 35
  • 46
  • damn it you were faster xD take my medal! – Andreas Oct 01 '14 at 20:14
  • Can you please expand this. How do I then retrieve values from $entity? I've tried both `$entity->variable` and `$entity->{"variable"}`, but neither are working. – user3420034 Oct 01 '14 at 22:29
  • Look at `json_decode()` [documentation](http://php.net/manual/en/function.json-decode.php). The second parameter determines whether to return associative array. This should work `$entity['variable1']`. – pgiecek Oct 01 '14 at 22:42
0

You can try to use the following code instead of your String variable payload:

List<NameValuePair> payload = new ArrayList<NameValuePair>();

payload.add(new BasicNameValuePair("variable1", "value1");

That worked for me