接收JSON POST [英] Receiving a JSON POST

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

问题描述

可能重复:
如何在php中获取POST正文?

Possible Duplicate:
How to get body of a POST in php?

我收到包含JSON的POST,问题是当我收到$ _POST为空时.为了测试何时收到POST,我创建了一个包含$ _POST,$ _ GET和$ _REQUEST的文件,它们都是空的.

I'm receiving a POST that contains a JSON, the problem is when I receive that the $_POST is empty. In order to test when I receive the POST I create a file that contais the $_POST, $_GET and $_REQUEST and they are all empty.

正在发送请求的客户端正在执行以下操作:

The client that is sending the request is doing something like this:

$itemJson = '{
      "id": 00,
      "value": "ok"
    }';

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: '. strlen($itemJson))
    );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $itemJson);
    curl_close($ch);

说实话,因为没有设置参数,所以我并不在意如何获取这些数据.

To be honest I don't understant how I'm getting this data since there's no parameter set.

有什么想法吗?

推荐答案

按如下所示在PHP中尝试(当请求是application/json时,则不会将数据放入$ _POST)

Try in PHP as below (When request is an application/json, then you will not get data into $_POST)

var_dump(json_decode(file_get_contents("php://input")));

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

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