下面由thinkphp教程栏目给大家介绍ThinkPHP6 结合GuzzleHTTP发送HTTP请求,希望对需要的朋友有所帮助!
背景
thinkphp微信公众号程序主动调用微信的接口需要用到access_token,以及需要主动发送请求设置公众号菜单。
为什么选择GuzzleHTTP
Guzzle是一个PHP的HTTP客户端,用来轻而易举地发送请求,并集成到我们的WE@ u _ = \ C ] N *B服务上。
接口简单:构建查询语句、POST请求、分流上传下载大文件、使用HTTP co} 0 F + wokies、上传JSON数据等等。 发送同步或异步的请求均使用相同的接口。 使用_ Z n |PSR-7接口来请求、响应、分流,允许你使用其他兼容的PSR-7类库与Guzzle共同开发。 抽象了底层的HTTP传输,k v 7 / o允许你改变环境以及其他的代码,如:对cURL与PHP的流或socket并非重度依赖,非阻塞事件循环。 中间件y ^ k系统允许你创建构成客户端行为。- , W Z 9
Guzzle中文文档:https://guzzle-cn.readthedocs.io/zh_CN/latest/
安装GuzzleHTTP
1.安装composer+ B . K
因为thI d 5 O K ; finkphp6采用composer安装,所以我的环境上已经装好了composer,此处略过安装cs B J ] 0 9omposer方法。需要请自行百度。
2.安装Guzzle
进入到tp项目目录
cd /App1 q hlications/XAr + , S BMPP/htdo\ ; ; 3 c _ Dcs/tp1/tp
执行安装命令
composer require guzzlehttp/guzzle
3.在php.ini文档中打开extension=php_openssl.dll
发送http get示例: 3 Y G代码
1、在controller中引入GuzzleHttp
u@ W n G Rse GuzzleHttp\Clj j w O 7ient; use GuzzleHttp\Exc: 1 weption\GuzzleException;
2、下面的示例程序是在tp6中采用HTTP GET获取微信公众平台的access token
//微信公众平台获取access token url $url = 'https://api.weixin$ Q 3 +.qq.com/cgi-bin/token?'; //获取access tokE 9 # ven时需要携带的参数 $params = arrayk ] 1 b h( 'grant_tx } aype' => 'client6 v L a_credential', 'appid' => config('app.WECHAT.APPID'), 'secret' => c{ $ & / 2 Lonfig('ap0 s 4p.WECHAT.SECRET') ); $resp = null; try { //使用GuzzleHTTP发送get请求 $client = new Client(); $resp = $client->request('GET', $url= r : b z.http_build_query($params)); } catcx 7 0 @ # 9 ` L !h (GuzzleException $e){ print($e); } if (empty($resp)) { return null; } //获取微信公众平台t 7 + - 3 {的response $data = json_decode($resp->getBody(), true); if (isset($data['errcode']) && $data['errcode'] != 0) { throw new \think\Exception ($data['errmsg'], $data['errcode']); }
发送http post示例代码
用法` 2 H b # _ 0非常简单,直接看代码吧。
/** * 创建自定义菜单 */ public function menu() { r_ / K f n ~equire __DIR__ . '/../../vendor/? x J R 8 |autB 4 : ( joload.php'; //构建HTTP post JSON body数据 $data =[ ] N 6 f U s . h array( 'button' => array( array( 'type' => 'click'[ 6 _ - | 2, 'name' => '主菜单1', 'sub_button} o = 8 s' => array( arj @ j ` i : / Qray( 'Y X ] p c +type' => 'click', 'name' => '子菜单1', 'key' => self::MENU_MAIN_1_CHILD_1 ), array( 'h & | ; - Vtype' => 'view', 'name' => '百度', 'url' => 'https://www.baid@ Z : o s 6 [ y Zu.com' ) ) ), array( 'type' => 'click', 'name' => '主菜单2', 'sub_button' => a\ D Irray( arrB c j (ay( 'type' => 'click', 'name' => '子菜单1', 'key' => self::MENU_MAIN_2_CHILD_1 ), array( 'type' => 'view', 'name' => 'QQ', 'url' => 'http://www.qqy i 6 z.com' ) ) ), array( 'type' => 'click', 'name' => '主菜单3', 'key' => self::) N 8MENU_MAIN_3 ) ) ); //构造请求json body和header数据 $options = json_encode(R Q d$data, JSON_UNESCAPED_UNICODE); $jsoG A ( W B b :nData = [ 'body' => $options, 'headers' => ['content-type' => 'application/json'] ]; $resp = null; try { $client = new Client(); //生成微信公众号菜单需要调用的微信接口url $url = 'a / ~ Khttg S $ : l a &ps://ap* { * c /i.\ ] m y I n ,weixin.qq.com/u A _ T r Y ?cgi-bin/menu/create?access_token=' . $this->_getAccessToken(); //发送http post请求 $resp = $client->post($url, $jsonData); } catw = F % # vch (GuzzleException $e){ print($e); } if (empty($resp)) { return null; } echo $res% P 0 ; A yp->getBody(); }
相关推荐:最新的1Q C j M U b s b [0个thinkpB p % 2hp视频教程
以上就是详解ThinkPHP6结合GuzzleHTTP发送HTTP请求的详细内容,更多请关注php中文网其它相关文章!
声明:本文转载于:segmentfault,如有侵犯,请联系admin@php.cn删除