snsapi_userinfo方式获取用户信息config.php 存放appid和appsecret<?php $config = ['appid' => '************','appsecret' => '*******************',];test.php跳转并获取code<?php
require_once './config.php';
$appid = $config['appid'];
$redirect_uri = urlencode('http://www.haoyiya.cn/weixin/auth.php');
//授权url
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
//如果用户同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。
header("location:$url");
exit;auth.php获取用户信息<?phpheader ("Content-type:text/html;charset=utf-8");require_once './config.php';//获取codeif (isset($_GET['code'])){$code = $_GET['code'];}else{die("NO CODE");}$obj = new snsapi_userinfo;//获取access_token和openid$res = $obj->access_token($config['appid'],$config['appsecret'],$code);//获取用户信息$userInfo = $obj->userInfo($res->access_token,$res->openid);//输出用户信息echo "