PHP微信扫码登录
本文使用php 进行微信pc 扫码登录,扫码获取用户信息
话不多说直接上代码吧!
怎么使用在代码最下面
<?php class WeChatRcLogin { public $state = ''; public $appid = ''; public $secret = ''; public $redirect_uri = ''; public $error = ''; public $data = []; public function __construct($config) { $this->state = $config['state']; $this->appid = $config['appid']; $this->secret = $config['secret']; $this->redirect_uri = $config['redirect_uri']; } /** * 获取登录的url * @return string * @author: wmq * @Time: 2022/10/13 14:40 */ public function getLoginUrl(){ $arr = [ 'appid' => $this->appid, // appid 'redirect_uri' => $this->redirect_uri, //回调url 'response_type' => 'code', 'scope' => 'snsapi_login', 'state' => $this->state, //回调验签数据 ]; return 'https://open.weixin.qq.com/connect/qrconnect?'.http_build_query($arr).'#wechat_redirect'; } /** * 获取access_token * @param $param * @return bool * @author: wmq * @Time: 2022/10/13 14:40 */ public function getAccessToken($param){ if(!isset($param['code'],$param['state'])){ $this->error = '缺少参数'; return false; } $arr = [ 'appid' => $this->appid, 'secret' => $this->secret, 'code' => $param['code'], 'grant_type' => 'authorization_code' ]; $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?'.http_build_query($arr); $result = Curl::get($url); if(isset($result['errcode'])){ $this->error = $result['errmsg']; return false; }else{ if($this->state && $param['state'] != $this->state){ $this->error = '验签失败'; return false; } $this->data = $result; return true; } } /** * 获取用户信息 * @param $param * @return bool * @author: wmq * @Time: 2022/10/13 14:40 */ public function getUserInfo($param){ if(!isset($param['access_token'],$param['openid'])){ $this->error = '缺少参数'; return false; } $arr = [ 'access_token' => $param['access_token'], 'openid' => $param['openid'], ]; $url = 'https://api.weixin.qq.com/sns/userinfo?'.http_build_query($arr); $result = Curl::get($url); if(isset($result['errcode'])){ $this->error = $result['errmsg']; return false; }else{ $this->data = $result; return true; } } } class Curl { /** * curl post 请求 * @param string $url 地址 * @param array $data 数据 * @return bool|string * @author: wmq * @Time: 2022/4/25 9:18 */ public static function post($url, $data = [],$header = []){ return self::curl_request($url,$data,$header); } /** * get请求 * @param string $url 地址 * @param int $timeout 时间 * @return bool|string * @author: wmq * @Time: 2022/4/25 9:21\ */ public static function get($url,$header = []){ return self::curl_request($url,[],$header); } public static function curl_request($url,$data = [],$header) { !$header && $headerArray = ["Content-type:application/json;charset='utf-8'","Accept:application/json"]; $header && $headerArray = $header; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 30); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray); if (!empty($data)) { curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } $output = curl_exec($curl); curl_close($curl); $res = json_decode($output,true); return $res; } } //使用方法 $config = [ 'appid' => '', //appid 微信开放平台获取 'secret' => '', // secret 微信开放平台获取 'redirect_uri' => '', // 回调地址,微信开放平台设置,扫码成功后微信调到并携带code,state的地址 'state' => '' // 回调签名验签 可以为空,可以存到缓存中验证 ]; $arr = []; $l = new WeChatRcLogin($config); //获取跳转地址 // $l->getLoginUrl(); //https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Authorized_Interface_Calling_UnionID.html //access_token 建议根据expires_in 进行缓存 获取access_token 是有次数限制的,明细请看官网文档 //获取用户信息 if($l->getAccessToken($_GET)){ //获取access_token 根据state, code 微信回调会传过来, $data = $l->data; //access_token 信息 if($l->getUserInfo($l->data)){ // 获取用户信息 echo '获取成功<br>'; $arr = array_merge($arr,$l->data); //合并access_token 返回的东西 + 用户信息返回的东西 var_dump($arr); }else{ //获取用户信息失败 echo '获取用户信息失败<br>'; var_dump($l->error); } }else{ echo '获取access_token失败<br>'; var_dump($l->error); echo '<br>'; }