记:第一次使用guzzle+thinkcmf,舍弃手写CURL采用PHP流处理发送HTTP请求

 

1.cmd切换目录到thinkcmf根项目中,然后composer require guzzlehttp/guzzle引入;

 

2.查看官方文档(https://guzzle-cn.readthedocs.io/zh_CN/latest/overview.html),根据自己的需求封装了适合自己的guzzle,代码如下

 /**
     * guzzle curl请求,目前只支持抛出post+json 和 get+query
     * @param string $Uri    域名之后的地址,详情见:https://guzzle-cn.readthedocs.io/zh_CN/latest/quickstart.html#id2
     * @param array $header  请求头部 自定义
     * @param array $params     请求数据
     * @param string $method  请求方式 默认POST
     * @return bool false|true
     */
    protected function request($Uri="",$header=[],$params=[],$method="POST"){
        $config = ["base_uri"=>$this->baseUrl,'verify' => false];
        $Client = new Client($config);
        $data = ['connect_timeout' => 30, 'headers' => $header];
        if($method == 'POST'){
            $data['json'] = $params;
        }else{
            $data['query'] = $params;
        }
        $result = $Client->request($method,$Uri,$data);
        return $result->getBody();
    }

记得use guzzle类哦,哈哈。

 

 设置 'verify' => false,忽略请求https 报错(guzzle 报错ssl)

 

 

先前自己请求接口都是使用的CURL(感觉实属落伍了),一次项目中自己接口(PHP)中有包含了第三方接口,然后.net请求总是报服务器错误PHP请求又是正常的,纠结的好久请教了某位很厉害的大佬,

推荐使用guzzle,瞬间就d爆了,完美解决;第一次写不习勿喷

 

posted @ 2021-03-10 17:14  PHP小骚年  阅读(272)  评论(0编辑  收藏  举报