php使用curl-post提交json格式數據(報錯)

你可以把之前 json_encode($data);改爲http_build_query($data);

至於原因,請去看詳解:https://segmentfault.com/a/1190000006220620

如果還不可以的話,你可以這樣

 public function hashChain($a,b,c){
        $url = "";
        $ch = curl_init();
        $post_data = array('a'=> $a, 'b'=> $b,'c' => $c, 'Time'=> time() );
        $data_string = json_encode($post_data);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // post數據
        curl_setopt($ch, CURLOPT_POST, 1);
        // post的變量
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
       //加入以下設置
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
        );

        $output = curl_exec($ch);
        curl_close($ch);
        //打印獲得的數據
        return json_decode($output);
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章