详解ThinkPHP6结合GuzzleHTTP发送HTTP请求

 6354

下面给大家介绍ThinkPHP6 结合GuzzleHTTP发送HTTP请求,希望对需要的朋友有所帮助!


背景

thinkphp微信公众号程序主动调用微信的接口需要用到access_token,以及需要主动发送请求设置公众号菜单。

为什么选择GuzzleHTTP

Guzzle是一个PHP的HTTP客户端,用来轻而易举地发送请求,并集成到我们的WEB服务上。

接口简单:构建查询语句、POST请求、分流上传下载大文件、使用HTTP cookies、上传JSON数据等等。

发送同步或异步的请求均使用相同的接口。

使用PSR-7接口来请求、响应、分流,允许你使用其他兼容的PSR-7类库与Guzzle共同开发。

抽象了底层的HTTP传输,允许你改变环境以及其他的代码,如:对cURL与PHP的流或socket并非重度依赖,非阻塞事件循环。

中间件系统允许你创建构成客户端行为。

Guzzle中文文档:https://guzzle-cn.readthedocs.io/zh_CN/latest/


安装GuzzleHTTP

1、安装composer

因为thinkphp6采用composer安装,所以我的环境上已经装好了composer,此处略过安装composer方法。需要请自行百度。


2、安装Guzzle

进入到tp项目目录

  1. cd /Applications/XAMPP/htdocs/tp1/tp

执行安装命令

  1. composer require guzzlehttp/guzzle

3、在php.ini文档中打开 extension=php_openssl.dll


发送http get示例代码

1、在controller中引入GuzzleHttp

  1. use GuzzleHttp\Client;
  2. use GuzzleHttp\Exception\GuzzleException;

2、下面的示例程序是在tp6中采用HTTP GET获取微信公众平台的access token

  1. //微信公众平台获取access token url
  2. $url = 'https://api.weixin.qq.com/cgi-bin/token?';
  3. //获取access token时需要携带的参数
  4. $params = array(
  5.     'grant_type' => 'client_credential',
  6.     'appid' => config('app.WECHAT.APPID'),
  7.     'secret' => config('app.WECHAT.SECRET')
  8. );
  9. $resp = null;
  10. try {
  11.     //使用GuzzleHTTP发送get请求
  12.     $client = new Client();
  13.     $resp = $client->request('GET', $url.http_build_query($params));
  14. } catch (GuzzleException $e){
  15.     print($e);
  16. }
  17.  
  18. if (empty($resp)) {
  19.     return null;
  20. }
  21. //获取微信公众平台的response
  22. $data = json_decode($resp->getBody(), true);
  23. if (isset($data['errcode']) && $data['errcode'] != 0) {
  24.     throw new \think\Exception ($data['errmsg'], $data['errcode']);
  25. }


发送http post示例代码

用法非常简单,直接看代码吧。

  1. /**
  2.  * 创建自定义菜单
  3.  */
  4. public function menu()
  5. {
  6.     require __DIR__ . '/../../vendor/autoload.php';
  7.     //构建HTTP post JSON body数据
  8.     $data = array(
  9.         'button' => array(
  10.             array(
  11.                 'type' => 'click',
  12.                 'name' => '主菜单1',
  13.                 'sub_button' => array(
  14.                     array(
  15.                         'type' => 'click',
  16.                         'name' => '子菜单1',
  17.                         'key' => self::MENU_MAIN_1_CHILD_1
  18.                     ),
  19.                     array(
  20.                         'type' => 'view',
  21.                         'name' => '百度',
  22.                         'url' => 'https://www.baidu.com'
  23.                     )
  24.                 )
  25.             ),
  26.             array(
  27.                 'type' => 'click',
  28.                 'name' => '主菜单2',
  29.                 'sub_button' => array(
  30.                     array(
  31.                         'type' => 'click',
  32.                         'name' => '子菜单1',
  33.                         'key' => self::MENU_MAIN_2_CHILD_1
  34.                     ),
  35.                     array(
  36.                         'type' => 'view',
  37.                         'name' => 'QQ',
  38.                         'url' => 'http://www.qq.com'
  39.                     )
  40.                 )
  41.             ),
  42.             array(
  43.                 'type' => 'click',
  44.                 'name' => '主菜单3',
  45.                 'key' => self::MENU_MAIN_3
  46.             )
  47.         )
  48.     );
  49.     //构造请求json body和header数据
  50.     $options = json_encode($data, JSON_UNESCAPED_UNICODE);
  51.     $jsonData = [
  52.         'body' => $options,
  53.         'headers' => ['content-type' => 'application/json']
  54.     ];
  55.  
  56.     $resp = null;
  57.     try {
  58.         $client = new Client();
  59.         //生成微信公众号菜单需要调用的微信接口url
  60.         $url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=' . $this->_getAccessToken();
  61.         //发送http post请求
  62.         $resp = $client->post($url, $jsonData);
  63.     } catch (GuzzleException $e){
  64.         print($e);
  65.     }
  66.  
  67.  
  68.     if (empty($resp)) {
  69.         return null;
  70.     }
  71.  
  72.     echo $resp->getBody();
  73. }


本文网址:https://www.zztuku.com/detail-8872.html
站长图库 - 详解ThinkPHP6结合GuzzleHTTP发送HTTP请求
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐