接收JSON POST

fre*_*d00 4 php post json curl

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

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

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

$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);
Run Code Online (Sandbox Code Playgroud)

说实话,我不明白我是如何得到这些数据的,因为没有参数设置.

有任何想法吗?

GBD*_*GBD 18

尝试PHP如下(当请求是一个应用程序/ json,那么你将不会获得数据到$ _POST)

var_dump(json_decode(file_get_contents("php://input")));
Run Code Online (Sandbox Code Playgroud)


Vol*_*erK 5

尝试

$request_body = file_get_contents('php://input');
$json = json_decode($request_body);
Run Code Online (Sandbox Code Playgroud)

在你的接收脚本中.

另见:http://docs.php.net/wrappers.php.php