我使用的是php 7.2
1、安装easywechat
参考:/docs/4.x/installation
cmd导航到网站目录:
cdD:\phpstudy_pro\WWW\
安装easywechat:
composer require overtrue/wechat:~4.0 -vvv
2、获取粉丝信息
参考:/docs/4.x/official-account/oauth
在home应用(也可以是其它应用)新建控制器:Server.php
内容如下:
<?phpdeclare (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