thinkphp5.0 CURL用post请求接口数据

时间:2018-11-15 15:56:25   收藏:0   阅读:1353
    //测试 请求接口
    public function  index(){
        $arr = array(‘a‘=>‘555‘,‘b‘=>56454564);
        $data=$this->post_json_data(json_encode($arr));
        dump(json_decode($data[‘result‘],true));
    }

    //测试 接口
    public function postTest(){
         //显示获得的数据
        if($this->request->isPost()){
            $arr = array(‘a‘=>‘666666‘,‘b‘=>999999);
            return json_encode($arr);
        }

    }
    /*
     * post 发送JSON 格式数据
     * @param $url string URL
     * @param $data_string string 请求的具体内容
     * @return array
     *      code 状态码
     *      result 返回结果
     */
    function post_json_data($url=‘ https://apiproxy-uat.ctripqa.com/apiproxy/soa2/13077/json/orderInfoCallbackV2‘, $data_string) {
        //初始化
        $ch = curl_init();
        //设置post方式提交
        curl_setopt($ch, CURLOPT_POST, 1);
        //设置抓取的url
        curl_setopt($ch, CURLOPT_URL, $url);
        //设置post数据
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        //设置头文件的信息作为数据流输出
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                ‘Content-Type: application/json; charset=utf-8‘,
                ‘Content-Length: ‘ . strlen($data_string))
        );
        ob_start();
         //执行命令
        curl_exec($ch);
        $return_content = ob_get_contents();
        ob_end_clean();
        $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        return array(‘code‘=>$return_code, ‘result‘=>$return_content);
    }

 个人记录一下哈

原文:https://www.cnblogs.com/zgbcode/p/9963994.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!