首页 >后端开发 >php教程 >调用API,对方总是说参数放在BODY里面

调用API,对方总是说参数放在BODY里面

WBOY
WBOY原创
2016-08-08 09:06:572779浏览

请问PHP CURL post json参数如何放在body里面

private function getAccessToken(){

<code>        $url='http://114.215.198.210:8081/api_v1/oauth2/accessToken';
        $postData=array('client_id'=>$this->cliendId,'client_secret'=>$this->client_secret,'grant_type'=>'client_credentials');
        echo $postJosnData = json_encode($postData);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postJosnData);    
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
        $data = curl_exec($ch);
        return $data;
    }
</code>

没错吧?对方总是说请求参数要放在BODY里面

回复内容:

请问PHP CURL post json参数如何放在body里面

private function getAccessToken(){

<code>        $url='http://114.215.198.210:8081/api_v1/oauth2/accessToken';
        $postData=array('client_id'=>$this->cliendId,'client_secret'=>$this->client_secret,'grant_type'=>'client_credentials');
        echo $postJosnData = json_encode($postData);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postJosnData);    
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
        $data = curl_exec($ch);
        return $data;
    }
</code>

没错吧?对方总是说请求参数要放在BODY里面

新增下面这句试试

<code> curl_setopt($ch, CURLOPT_HTTPHEADER, array(                   
        'Content-Type: application/json',  
        'Content-Length: ' . strlen($postData))           
    ); </code>

post请求body一般有两种格式,json和form。你用的json,可能对方用的form

php不懂,不过request的时候是可以把数据放到request body里的,看这个

http://stackoverflow.com/ques...

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn