在希望的田野上,我们看到了希望,看到了未来,看到了祖国未来繁荣昌盛的景象。
PHP微信登录类
<?php
/**
*作者:haodaima.com
*日期:2020-12-20
*时间:21:27
*/
class WeChatLogin
{
private $appid; /*微信公众号appid*/
private $secrete; /*微信公众号秘钥*/
private $GetCodeUri; /*获取code的链接*/
private $GetAeccessTokenUri; /*获取access 跟openID的链接*/
private $GetUserInfoUri; /*获取用户信息链接*/
private $code; /*微信返回的code*/
private $access_token; /*微信返回的access token*/
private $openid; /*微信返回的用户openid*/
private $redirect_uri; /*返回链接*/
/**
* WeChatLogin constructor.
* 构造函数,初始化必要参数
*/
public function __construct()
{
$wechat = include_once 'wechat_info.php';
$this->appid = $wechat['appid'];
$this->secrete = $wechat['secrete'];
$this->GetAeccessTokenUri = $wechat['GetAccessTokenUri'];
$this->GetCodeUri = $wechat['GetCodeUri'];
$this->GetUserInfoUri = $wechat['GetUserInfoUri'];
$this->redirect_uri = $wechat['redirect_url'];
}
/**
* 登录页面
*/
public function weChatLogin() {
$getcode = sprintf($this->GetCodeUri,$this->appid,urlencode($this->redirect_uri),md5(uniqid(rand(),true)));
/*当用户扫码确认后微信服务器会携带code值到redirect_uri上*/
header('location:'.$getcode);
exit();
}
/**
* 获取Code值
*/
public function getCode() {
/*当没有code值时说明用户取消了登录*/
if(!isset($_GET['code'])) {
exit('用户取消登录');
}else{
$this->code = $_GET['code'];
$this->getAccessToken(); /*调用获取access toke方法*/
}
}
/**
* 获取access token及openID
*/
public function getAccessToken() {
$getaccesstoken = sprintf($this->GetAeccessTokenUri,$this->appid,$this->secrete,$this->code);
$result = $this->curlRequest($getaccesstoken);
if(!isset($result['access_token']) && !isset($result['openid'])) {
exit('获取access token失败');
}else {
$this->access_token = $result['access_token'];
$this->openid = $result['openid'];
$this->getUserInfo(); /*调用获取用户信息接口*/
}
}
/**
* 拿到access token跟openID后获取用户信息
*/
public function getUserInfo() {
/*调用此方法时可从数据库检查有没有该openid对应的用户信息*/
$getuserinfo = sprintf($this->GetUserInfoUri,$this->access_token,$this->openid);
$userinfo = $this->curlRequest($getuserinfo);
/*打印用户信息 实际项目中需要将用户信息存入数据库*/
echo '<pre>';
print_r($userinfo);
echo '</pre>';
}
/**
* curl请求
* @param $url
* @return mixed
*/
private function curlRequest($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result,true);
}
}
/**
* 微信调用用户信息的接口有两种:
* 1、https://api.weixin.qq.com/sns/userinfo
* 2、https://api.weixin.qq.com/cgi-bin/user/info
* 上面两个接口都是获取用户信息的,并且传入的参数也是一样的,第一个是用户网页端,第二个使用公众号关注后获取用户信息。
*/
配置文件
<?php
/**
*作者:haodaima.com
*日期:2020-12-20
*时间:21:46
*/
return [
/*配置项 => 配置值*/
'appid' => '', /*微信公众号appid*/
'secrete' => '', /*微信公众号秘钥*/
'GetCodeUri' => 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=%s#wechat_redirect',
'GetAeccessTokenUri' => 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code',
'GetUserInfoUri' => 'https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN',
'redirect_uri' => 'https://www.ytymz.com/wechalogin/getcode'
];
以上就是PHP网页微信公众号登录。你活得不快乐的原因是:既无法忍受目前的状态,又没能力改变这一切,可以像只猪一样懒,却无法像只猪一样懒得心安理得。更多关于PHP网页微信公众号登录请关注haodaima.com其它相关文章!