简体   繁体   English

使用Java发布到PHP,以发送参数并从PHP读取参数

[英]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. 我有一个PHP,它是一种代理,它将接收来自某些客户端的请求,在本例中为使用HttpUrlConnection的Java类。 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). 一旦此PHP接收到连接(请求),PHP将提取客户端发送的参数,在我的情况下,这些参数是:一个id(字符串)和一个xml(字符串)。 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. 因此,一旦我的Java客户端发送了请求,我的php“代理”就会捕获该请求并提取参数,因为我的php就像一个“中间”组件,它使用curl将请求重定向到另一个地方,但是这个特定的php接收该请求需要提取de id和xml。 From java, I am using: 从Java,我正在使用:

        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: 对于我的php,我有以下内容:

$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. 我想从我的Java执行的帖子中获取数据,然后将它们作为字符串在PHP中使用。

If somebody could help me, I will really appreciate it. 如果有人可以帮助我,我将非常感激。 Thanks in advance, any help is welcome. 在此先感谢您的帮助。

You would want to use this in PHP: 您可能想在PHP中使用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM