0

I want to receive data in a Java program and send a json HTTP request to a PHP program to process further. I have written the following Java program.

import java.io.IOException;
import java.util.Scanner;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;


public class JSON_Test_1 
{
    public static void main(String args[]) throws IOException
    {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        Scanner obj = new Scanner(System.in);
        System.out.print ("Name: ");
        String name = obj.nextLine();

        //create a new JSON object
        JSONObject root = new JSONObject();
        //put the variables in JSON oebject

        root.put("name", name);
        System.out.println(root.toJSONString());

        try 
        {
            HttpPost request = new HttpPost("http://localhost/Test/");
            StringEntity params = new StringEntity(root.toString());
            request.addHeader("content-type", "application/json");
            request.setEntity(params);
            httpClient.execute(request);
        // handle response here...
        } 
        catch (Exception ex)
        {
            // handle exception here
        } 
        finally 
        {
            httpClient.close();
        }

    }    
}

Can anyone help me understand how can I decode the json object in the PHP program. Thanks in advance.

Pritam
  • 39
  • 5
  • http://php.net/manual/en/book.curl.php – Ceeee Feb 05 '16 at 02:58
  • 2
    Maybe with .... [`json_decode()`](http://www.php.net/json_decode)? – fusion3k Feb 05 '16 at 02:59
  • Thanks for the material. A little bit of code description will help me better. I am new to PHP and JSON handling. – Pritam Feb 05 '16 at 03:00
  • @fusion3k I want the PHP program to run instantly when the json request is send from the java program. And the PHP program should decode the json object. Could you please help me understand how it can be done ? – Pritam Feb 05 '16 at 03:02

1 Answers1

1

The data should be "in" the request input/body stream of the php instance, which you can access via php://input.
The (string) json data can be decoded via json_decode()

<?php
$data = json_decode( file_get_contents('php://input'), true );
echo $data['name'];
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • I am still unable to read the json object. When I run the java program it works fine but at the php end no display. – Pritam Feb 05 '16 at 03:16
  • What do you try to "display"? And how? – VolkerK Feb 05 '16 at 08:43
  • please check this post http://stackoverflow.com/questions/35691656/unable-to-receive-json-post-request-in-php/35691747#35691747 any advice here would be a great help. – Pritam Feb 29 '16 at 04:06
  • The question(s) still remain(s) `What do you try to "display"? And how?`. I don't see any (java) code that "displays" anything. – VolkerK Feb 29 '16 at 04:36
  • Yes, I have not given any Java code that displays anything. I first thought of testing if PHP is receiving variable from Java. What I need exactly is: Java program should send a JSON request to PHP and PHP after some operation should return the some variable to Java. Now, at the PHP end I am unable to print the JSON request from Java. Is there any possibility of doing this ? – Pritam Feb 29 '16 at 04:59
  • "Now, at the PHP end I am unable to print the JSON request" - _Exactly_ how did you determine _that_? If php prints/echos anything the output is sent to the client. Now if your java code doesn't use that output ...how do you know what the php script sent? – VolkerK Feb 29 '16 at 05:02
  • I tried checking with this statement if ($_SERVER['REQUEST_METHOD'] == 'POST') { $data = json_decode(file_get_contents("php://input")); echo($data); } else { echo "XXXXXX"; } It always executes the else part. I am not quite sure if PHP program is able to receive the JSON object. – Pritam Feb 29 '16 at 05:06
  • " It always executes the else part" - and my question is just: How do you know that? How did you check that? How? There are ways to do that, I just want to know which one you've used.... – VolkerK Feb 29 '16 at 05:07
  • After executing the Java program I refresh the PHP page which always executes the else part. I did not use any debugger for checking line execution. – Pritam Feb 29 '16 at 05:12
  • What? Refresh the php page? In your java application your instantiating a org.apache.http.client. This has absolutely nothing to do with your firefox/chrome/safari/whatever _browser_. So, where did you "refresh" the page? You have to fetch the response from the org.apache.http.client instance _in your java code_. – VolkerK Feb 29 '16 at 05:15
  • I am new to JSON handling. May be I am doing it the wrong way. If you check this URL http://stackoverflow.com/questions/35691656/unable-to-receive-json-post-request-in-php/35691747#35691747 I have given the code I use in Java and PHP end. If you could advice me with the right procedure It would be good. – Pritam Feb 29 '16 at 05:17