简体   繁体   English

无法在PHP中接收JSON POST请求

[英]Unable to receive JSON POST request in PHP

I am passing a JOSN object from Java to PHP. 我正在将JOSN对象从Java传递到PHP。 I am using jdk 1.8 ang WAMP server. 我正在使用jdk 1.8 ang WAMP服务器。 Below is the Java code. 以下是Java代码。

import java.io.IOException;
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.JSONObject;

/**
 *
 * @author PReeeT Dash
 */
public class FromJava 
{
    public static void main(String[] args) throws IOException
    {
        JSONObject json = new JSONObject();
        json.put("someKey", "someValue");    

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        try 
        {
            HttpPost request = new HttpPost("http://localhost/PGP/JSONReq/tophp.php");
            StringEntity params = new StringEntity(json.toString());
            request.addHeader("content-type", "application/json");
            request.setEntity(params);
            httpClient.execute(request);
        // handle response here...
        } catch (Exception ex) 
        {
            System.out.println("Error: Cannot Estabilish Connection");        
        } 
        finally 
        {
            httpClient.close();
        }
    }    
}

PHP script: PHP脚本:

$data = json_decode(file_get_contents("php://input"));
echo($data);

When I run the PHP file it always shows an Empty page. 当我运行PHP文件时,它总是显示一个空白页面。 Can anyone please help me understand why is it not working. 谁能帮我了解为什么它不起作用。

When I run the following PHP code it always executes the else condition. 当我运行以下PHP代码时,它将始终执行else条件。

if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        $data = json_decode(file_get_contents("php://input"));
        echo($data);
    }
    else
    {
        echo "XXXXXX";
    } 

I do not think this will work. 我认为这行不通。

A PHP script is not "listening" as would a WebService. PHP脚本不会像WebService那样“监听”。 However, upon receiving the request, the script processes it and try to "print" the result in HTML, not Java. 但是,收到请求后,脚本将对其进行处理,并尝试以HTML(而非Java)“打印”结果。

Fetch the response body your org.apache.http.client instance receives and eg send it to System.out 获取您的org.apache.http.client实例接收到的响应正文并将其发送到System.out

CloseableHttpResponse response = httpClient.execute(request);
IOUtils.copy(response.getEntity().getContent(), System.out);

For IOUtils use import org.apache.commons.io.IOUtils; 对于IOUtils,请使用import org.apache.commons.io.IOUtils; In case you're using maven the dependency is 如果您使用的是Maven,则依赖项为

<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.4</version>
</dependency>

You will most likely get the output 您很可能会得到输出

Catchable fatal error<:  Object of class stdClass could not be converted to string

because echo($data) doesn't work. 因为echo($data)不起作用。 json_decode(...) returns a stdClass. json_decode(...)返回一个stdClass。 Try 尝试

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    // json_decode(..., true) will return an array instead of a stdClass
    $data = json_decode(file_get_contents("php://input"), true);
    var_export($data);
}
else
{
    var_export($_SERVER['REQUEST_METHOD']);
}

instead. 代替。

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

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