Marcelo Tataje
Marcelo Tataje

Reputation: 3871

Post with Java to PHP sending parameters and read parameters from PHP

I know that there are many questions related to this, but I have a special scenario and I'm here to know if somebody could help me with it, I have an approach to what is required, so let me describe the scenario first:

I have a PHP which is a kind of proxy, it will receives requests from some client, in my case a Java class using HttpUrlConnection. As soon as this PHP receives the connection (request), the php will extract the parameters sent by the client, in my case, the parameters are: an id (String) and an xml (as String). So, as soon as my java client sends the request, my php "proxy" catch it and extract the parameters, because my php is like an "intermediate" component which redirects the requests to another place using curl, but this specific php which receives the request needs to extract de id and the xml. From java, I am using:

        URL calledUrl = new URL(phpUrl);
        URLConnection phpConnection = calledUrl.openConnection();

        HttpURLConnection httpBasedConnection = (HttpURLConnection) phpConnection;
        httpBasedConnection.setRequestMethod("POST");
        httpBasedConnection.setDoOutput(true);
        StringBuffer paramsBuilder = new StringBuffer();
        paramsBuilder.append("xmlQuery=");
        paramsBuilder.append(URLEncoder.encode(this.xmlQuery, CHARSET));
        paramsBuilder.append("&ids=");
        paramsBuilder.append(URLEncoder.encode("16534", CHARSET));

        PrintWriter requestWriter = new PrintWriter(httpBasedConnection.getOutputStream(), true);
        requestWriter.print(paramsBuilder.toString());
        requestWriter.close();

        BufferedReader responseReader = new BufferedReader(new InputStreamReader(
                phpConnection.getInputStream()));

        String receivedLine;
        StringBuffer responseAppender = new StringBuffer();

        while ((receivedLine = responseReader.readLine()) != null ) {
            responseAppender.append(receivedLine);
            responseAppender.append("\n");
        }
        responseReader.close();
        result = responseAppender.toString();   

As you can see, I am sending the parameters and waiting for a response (which is an echo).

For my php, I have the following:

$rawdata = file_get_contents('php://input');

$rawXml = simplexml_load_file('php://input');

I don't know if this is ok, but I saw a tutorial about receiving requests and these lines were there. I want to get the data from the post that my java executes and then work with them as strings in my php.

If somebody could help me, I will really appreciate it. Thanks in advance, any help is welcome.

Upvotes: 0

Views: 2229

Answers (1)

laz
laz

Reputation: 28638

You would want to use this in PHP:

$id = $_POST['id'];
$xml = $_POST['xmlQuery'];

Upvotes: 1

Related Questions