0

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.

1 Answer 1

1

You would want to use this in PHP:

$id = $_POST['id'];
$xml = $_POST['xmlQuery'];
Sign up to request clarification or add additional context in comments.

7 Comments

thank you very much for the quick answer, I've added these lines in my php, but I my Java code does not print any response :/.
What do you want the response to contain?
Just to test, I want to see the same result of what I am sending:
$id = $_POST['ids'];$xmlQuery = $_POST['xmlQuery'];echo $id;echo $xmlQuery;
There is no newline in the output, try adding echo "\n";
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.