Php 使用curl发布json数据时的奇怪行为

Php 使用curl发布json数据时的奇怪行为,php,json,post,curl,Php,Json,Post,Curl,我使用curl将json数据发布到php中的rest端点。 这是在集成测试中完成的。 令人惊讶的是,整个json对象在服务器上的$\u POST中对我是可用的。 这怎么可能? 下面是我发布json的php代码: $data = array( 'username' => 'blahbah', 'password' => 'blahblah', 'grant_type' => 'client_credentia

我使用curl将json数据发布到php中的rest端点。 这是在集成测试中完成的。 令人惊讶的是,整个json对象在服务器上的$\u POST中对我是可用的。 这怎么可能? 下面是我发布json的php代码:

$data = array(
            'username' => 'blahbah',
            'password' => 'blahblah',
            'grant_type' => 'client_credentials',
            'client_id' => 'blah',
            'client_secret' => 'blah'
        );
        $data_string = json_encode($data);

        $this->curl = curl_init($this->tokenApi);
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($this->curl, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data_string));
        $response = curl_exec($this->curl);
在api方面,如果我进行打印($\u POST),我会得到:

这怎么可能? 据我所知,json字符串应该只能作为“php//input”使用。我对访问json的正确方式有点困惑。我使用的是PHP5.4


谢谢。

为什么该json字符串在
$\u POST
superglobal中不可用?您可以将其作为post值发送。即使内容类型设置为application/json,为什么会有不同?请注意,这里谈论的是http请求,而不是http响应!如果调用php脚本,http服务器将把请求负载移交给php脚本。除非您以某种方式将YoutHTTP服务器配置为不这样做。PHP在这里并没有看到任何区别。也许您正在将其与您在例如jqueries ajax便利函数中看到的行为进行比较,如果为请求指定了解码的json类型,则该函数会发出解码的json。但这仅仅是实现这些便利功能的问题。如果愿意,也可以在php中执行同样的操作。或者从post值中获取json字符串并对其进行解码。我看不出这里有什么问题……实际上,你问题的答案写在你引用的那个老问题上。。。“CURLOPT_POSTFIELDS告诉curl将参数编码为application/x-www-form-urlencoded”
Array
(
    [username] => blahbah
    [password] => blahblah
    [grant_type] => client_credentials
    [client_id] => blah
    [client_secret] => blah
)