本文使用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