无法在PHP中接收JSON POST请求 [英] Unable to receive JSON POST request in PHP

查看:121
本文介绍了无法在PHP中接收JSON POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个JOSN对象从Java传递给PHP。我正在使用jdk 1.8 ang WAMP服务器。
下面是Java代码。

I am passing a JOSN object from Java to PHP. I am using jdk 1.8 ang WAMP server. Below is the Java code.

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脚本:

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

当我运行PHP文件时,它总是显示一个空页面。任何人都可以帮助我理解为什么它不起作用。

When I run the PHP file it always shows an Empty page. Can anyone please help me understand why is it not working.

当我运行以下PHP代码时,它总是执行else条件。

When I run the following PHP code it always executes the else condition.

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


推荐答案

获取您的组织的响应正文.apache.http.client实例接收和例如发送到System.out

Fetch the response body your org.apache.http.client instance receives and e.g. send it to System.out

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

对于IOUtils,使用 import org.apache.commons.io.IOUtils;
如果您正在使用maven,则依赖性为

For IOUtils use import org.apache.commons.io.IOUtils; In case you're using maven the dependency is

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

您很可能获得输出

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

因为 echo($ data)不起作用。 json_decode(...)返回一个stdClass。
尝试

because echo($data) doesn't work. json_decode(...) returns a 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']);
}

而不是。

这篇关于无法在PHP中接收JSON POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆