thinkphp 6.x 利用 easywechat获取微信公众号粉丝信息

21 篇文章 0 订阅
5 篇文章 1 订阅

我使用的是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 -vvv

2、获取粉丝信息

参考: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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值