作者:糖糖6 | 来源:互联网 | 2023-05-17 10:09
所有微信开发的相关内容,都需要参考官方文档。[微信公众平台|开发文档]http:mp.weixin.qq.comwikihome。一、通过网页授权,可以获取用户微信的基本信息。
所有微信开发的相关内容,都需要参考官方文档。
[微信公众平台|开发文档] http://mp.weixin.qq.com/wiki/home/。
一、通过网页授权,可以获取用户微信的基本信息。
二、总共有5个步骤:
1 :用户同意授权,获取code
2 :通过code换取网页授权access_token
3 :刷新access_token(如果需要)
4 :拉取用户信息(需scope为 snsapi_userinfo)
5 附:检验授权凭证(access_token)是否有效
三、每一个步骤的实现。
1 :用户同意授权,获取code
- public function getCode()
- {
- if (isset($_GET["code"])) {
- return $_GET["code"];
- } else {
- $str = "location: https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->appid . "&redirect_uri=" . $this->index_url . "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
- header($str);
- exit;
- }
- }
2 :通过code换取网页授权access_token
- public function getOpenId()
- {
- $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->appid . "&secret=" . $this->appsecret . "&code=" . $this->code . "&grant_type=authorization_code";
- $access_token_json = $this->https_request($access_token_url);
- $access_token_array = json_decode($access_token_json, TRUE);
- return $access_token_array;
- }
3 :刷新access_token(如果需要)
这一步直接略过。
4 :拉取用户信息(需scope为 snsapi_userinfo)
- public function getUserInfo()
- {
-
- $userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$this->access_token['access_token'] ."&openid=" . $this->access_token['openid']."&lang=zh_CN";
- $userinfo_json = $this->https_request($userinfo_url);
- $userinfo_array = json_decode($userinfo_json, TRUE);
- return $userinfo_array;
- }
至此四个步骤全部完成。
四、完整代码。实际项目使用TP3.2.3。
-
-
- namespace Wechat\Controller;
-
- use Think\Controller;
-
- class WxbaseController extends Controller
- {
-
- public $appid = 'wxba09d9f0fed4b84b';
- public $appsecret = '332c2b1fc1eb282c0136b73723db4237';
- public $index_url = "http://www.你的域名.cn/项目目录/index.php?m=分组&c=控制器&a=方法"; //微信回调地址,要跟公众平台的配置域名相同
- public $code;
- public $openid;
-
- /**
- *检测有无$_SESSION。"font-family: Arial, Helvetica, sans-serif;">如果有,直接忽略。
- *如果没有$"font-family:Arial, Helvetica, sans-serif;">_SESSION,就依次执行getCode、getOpenId、getUserInfo来获取用户信息。目的是解决CODE只能获取一次,刷新页面openid会丢失的问题。
- *再判断是否在数据库中,没有则写入数据库。最后将open_id写入session。
- "white-space:pre"> */
- public function _initialize()
- {
- if (!$_SESSION['openid']) {
- $this->code = $this->getCode();
- $this->access_token = $this->getOpenId();
- $userInfo = $this->getUserInfo();
- if ($userInfo) {
- $ins = M('Wechat_user_info');"white-space:pre">
- $map['openid'] = $userInfo['openid'];
- $result = $ins->where($map)->find();
- if (!$result) {
- $ins->add($userInfo);
- }
- session('openid', $userInfo['openid']);
- }
- }
-
- }
-
-
- public function getCode()
- {
- if (isset($_GET["code"])) {
- return $_GET["code"];
- } else {
- $str = "location: https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->appid . "&redirect_uri=" . $this->index_url . "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
- header($str);
- exit;
- }
- }
-
-
- public function getOpenId()
- {
- $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->appid . "&secret=" . $this->appsecret . "&code=" . $this->code . "&grant_type=authorization_code";
- $access_token_json = $this->https_request($access_token_url);
- $access_token_array = json_decode($access_token_json, TRUE);
- return $access_token_array;
- }
-
-
- public function getUserInfo()
- {
-
- $userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$this->access_token['access_token'] ."&openid=" . $this->access_token['openid']."&lang=zh_CN";
- $userinfo_json = $this->https_request($userinfo_url);
- $userinfo_array = json_decode($userinfo_json, TRUE);
- return $userinfo_array;
- }
-
-
-
- public function https_request($url, $data = null)
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
- if (!empty($data)) {
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- }
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $output = curl_exec($curl);
- curl_close($curl);
- return $output;
- }
- }