請問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...