thinkphp 6.x 利用 easywechat获取微信公众号粉丝信息
生活随笔
收集整理的這篇文章主要介紹了
thinkphp 6.x 利用 easywechat获取微信公众号粉丝信息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我使用的是php 7.2
1、安裝?easywechat
? ? ?參考:https://www.easywechat.com/docs/4.x/installation
cmd導航到網站目錄:
cd??D:\phpstudy_pro\WWW\www.mydomain.com安裝easywechat:
composer require overtrue/wechat:~4.0 -vvv2、獲取粉絲信息
參考:https://www.easywechat.com/docs/4.x/official-account/oauth
在home應用(也可以是其它應用)新建控制器:Server.php
內容如下:
<?php declare (strict_types = 1);namespace app\home\controller; use think\facade\View; use EasyWeChat\Factory; use EasyWeChat\Kernel\Messages\Message; use EasyWeChat\Kernel\Messages\Text;class Server { private $config = ['app_id' => '你的app_id','secret' => '你的secret','token' => '你的token', // Token'aes_key' => '你的EncodingAESKey', // EncodingAESKey,兼容與安全模式下請一定要填寫!!!// 指定 API 調用返回結果的類型:array(default)/collection/object/raw/自定義類名'response_type' => 'array',//...];public function wechat(){$app = Factory::officialAccount($this->config);$oauth = $app->oauth; $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';//echo $http_type . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; die;//回調地址$callback_url = $http_type . $_SERVER['HTTP_HOST'].'/home/server/wechatres'; $response = $oauth->scopes(['snsapi_userinfo'])->redirect($callback_url); // 將響應輸出$response->send(); }public function wechatres(){$app = Factory::officialAccount($this->config);$oauth = $app->oauth;// 獲取 OAuth 授權結果用戶信息$user = $oauth->user();//print_r($user);$openid = $user['id'];$name = $user['name'];$nickname = $user['nickname'];$avatar = $user['avatar'];$email = $user['email'];$original = $user['original'];$sex = $original['sex'];$city = $original['city'];$province = $original['province'];$country = $original['country'];echo $user->getId();} }3、在微信打開地址測試:
http://你的域名/home/server/wechat
總結
以上是生活随笔為你收集整理的thinkphp 6.x 利用 easywechat获取微信公众号粉丝信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对比homeassistant和智汀家庭
- 下一篇: 锐龙R3-3300X和i5-9400f哪