Home > Backend Development > PHP Tutorial > When calling the API, the other party always says that the parameters are placed in the BODY.

When calling the API, the other party always says that the parameters are placed in the BODY.

WBOY
Release: 2016-08-08 09:06:57
Original
2784 people have browsed it

Please tell me how to put the PHP CURL post json parameters in the 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>
Copy after login
Copy after login

That’s right? The other party always says that request parameters should be placed in BODY

Reply content:

Please tell me how to put the PHP CURL post json parameters in the 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>
Copy after login
Copy after login

That’s right? The other party always says that request parameters should be placed in BODY

Add the following sentence and try it

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

Post request body generally has two formats, json and form. The json you use may be the form the other party uses

I don’t understand PHP, but you can put data in the request body when making a request. Look at this

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

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template