当前位置: 首页 > 工具软件 > EasyWeChat > 使用案例 >

easywechat6获取微信用户信息(基于公众号)

蔺劲
2023-12-01
//当前页面 redirect.php

require './vendor/autoload.php';

use EasyWeChat\OfficialAccount\Application;

$config = [
    'app_id' => '公众号appID',
    'secret' => '公众号appsecret',
    'token' => '公众号token',
    'aes_key' => '' //明文模式下为空
];

$app = new Application($config);

$oauth = $app->getOauth();

// 未登录
if (empty($_GET['code'])) {
  //生成完整的授权URL
  $redirectUrl = $oauth->redirect('http://www.xxx.com/demo/redirect.php');
  header("Location: {$redirectUrl}");
  exit;
} else {
  // 已经登录过,则从userFromCode方法中取用户信息
  $user = $oauth->userFromCode($_GET['code']);
  //var_dump($user);
  echo 'openid:'.$user['id'].'<br>昵称:'.$user['nickname'].'<br>';
  echo '<img src="'.$user['avatar'].'">'; //用户头像
}

注意事项:

访问https://curl.haxx.se/docs/caextract.html,下载cacert.pem,并在php.ini文件添加

curl.cainfo="D:/wamp/bin/php/php8.1/extras/ssl/cacert.pem"
openssl.cafile="D:/wamp/bin/php/php8.1/extras/ssl/cacert.pem"

 类似资料: