php curl post 请求接口,要求数据json格式,带中文解决请求失败,记一下。

今天请求一个接口。postman测试可以调通,但是用curl 一直报错。最后是CURLOPT_HTTPHEADER   head头没有定义编码。

function getZhongliangapi($url,$data){

        $string='';

        $signstring = json_encode($data);

        ksort($data, 2);

        foreach($data as $s){
            $string.=$s.'&';
        }

        $string.=$this->key;


        $signature = hash("sha256", $string);


        $ch = curl_init();
        $headers = array(

            "Content-type: application/json;charset='utf-8'",

           // "Accept: application/json",
          //  "Cache-Control: no-cache",
           // "Pragma: no-cache",
        );
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_URL, $url."$signature");

        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $signstring);
        ob_start();
        $result = curl_exec ($ch);
        ob_end_clean();
        curl_close ($ch);
        unset($ch);
        return $result;
    }

猜你喜欢

转载自blog.csdn.net/weixin_40896800/article/details/81563154
今日推荐