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

时间:2015-07-05 06:57:04

标签: php json post curl

我正在使用curl将json数据发布到php中的rest端点。 这是在集成测试中完成的。 令人惊讶的是,整个json对象在服务器上的$ _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方面,如果我执行print_r($ _ POST),我会得到:

Array
(
    [username] => blahbah
    [password] => blahblah
    [grant_type] => client_credentials
    [client_id] => blah
    [client_secret] => blah
)

这怎么可能? 根据我的阅读,json字符串应仅作为“php // input”提供。我对访问json的正确方法有点困惑。我使用的是PHP 5.4。

感谢。

0 个答案:

没有答案
相关问题