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

 雅丽猫儿 发布于 2022-11-15 09:59

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

private function getAccessToken(){

        $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;
    }

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

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

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

    2022-11-15 10:03 回答
  • post请求body一般有两种格式,json和form。你用的json,可能对方用的form

    2022-11-15 10:03 回答
  • 新增下面这句试试

     curl_setopt($ch, CURLOPT_HTTPHEADER, array(                   
            'Content-Type: application/json',  
            'Content-Length: ' . strlen($postData))           
        ); 
    2022-11-15 10:03 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有