当前位置 博文首页 > json_li的博客:PHP 实现微信登录
因为近期项目开发用到微信登录功能,在这里记录一下。
(1) 第三方发起微信授权登录请求,会生成微信登录二维码,微信用户扫描登录后,微信重定向到第三方网站,并且带上授权临时票据code参数;
(2)通过code参数加上AppID和AppSecret,通过API换取access_token和openid;
(3)通过access_token和openid进行接口调用,获取用户基本信息三方网站实现用户登录。
实现思路:
首先请求微信登录二维码,传过去回调地址
在微信回调时,接收code参数,并通过code获取用户access_token和openid
再通过access_token和openid请求微信API得到用户的信息,最后返回给客户端
路由
//微信网页授权调试
Route::get('getWeChatCode', 'Controller@getWeChatCode');
//获取微信回调信息
Route::get('weChatCallback', 'Controller@weChatCallback');
控制器
/**
* 网页登录授权
*/
public function getWeChatCode()
{
$object = new WeChat();
$callback_url = request()->input('url');
$res = $object->getKFLoginUrl($callback_url);
echo "<a href='" . $res . "'>点击</a>";
}
/**
* 回调获取用户信息
*/
public function weChatCallback()
{
$code = request()->input('code');
$object = new WeChat();
$openInfo = $object->getKFOpenId($code);
if (isset($openInfo['errcode'])) {
echo '微信登录登录失败' . $openInfo['errmsg'];die;
}
$userInfo = $object->getUserInfo($openInfo['access_token'], $openInfo['openid']);
echo '登录用户信息:';
print_r($userInfo);
}
微信类方法
class WeChat
{
/**
* 微信开放平台appid
* @var string
*/
protected static $kF_AppId = 'xxxxxxxxxx';
/**
* 微信开放平台app secret
* @var string
*/
protected static $KF_AppSecret = 'xxxxxxxxxxxxx';
/**
* 通过开放平台key获取微信登录页面
* 可通过回调获取code参数
* @param $callback_url:回调地址
* @return string
*/
public function getKFLoginUrl($callback_url)
{
$callback = urlencode($callback_url);
$AppId = self::$kF_AppId;
$get_code_url = "https://open.weixin.qq.com/connect/qrconnect?appid={$AppId}&redirect_uri={$callback}&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect";
return $get_code_url;
}
/**
* 通过开放平台key
* 获取用户openId access_token
* @param $code
* @return bool|string
*/
public function getKFOpenId($code)
{
$AppId = self::$kF_AppId;
$AppSecret = self::$KF_AppSecret;
$get_openid_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$AppId}&secret={$AppSecret}&code={$code}&grant_type=authorization_code";
$res = file_get_contents($get_openid_url);
$res = json_decode($res, true);
return $res;
}
/**
* 获取微信用户信息
* @param $access_token
* @param $openId
* @return bool|mixed
*/
public function getUserInfo($access_token, $openId)
{
$url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openId}&lang=zh_CN";
$res = $this->linkCurl($url, 'GET');
$res = json_decode($res, true);
return $res;
}
/**
* 请求接口返回内容
* @param $url :请求的URL地址
* @param $method :请求方式POST|GET
* @param $params :请求的参数
* @param $header : 请求头
* @return bool|string
*/
protected function linkCurl($url, $method, $params = array(), $header = array())
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (strpos("$" . $url, "https://") == 1) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if ($method == "POST") {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
} else if ($params) {
curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($params));
}
$response = curl_exec($ch);
if ($response === false) {
return false;
}
curl_close($ch);
return $response;
}
}
点击后,扫描二维码进行登录
登录完成后,获取用户信息
?
如果微信登录和公众号网页授权都在一个项目中,就不能通过简单的通过微信用户openid来判断是否为同一个用户,因为这两个平台返回的同一个用户openid不一致,只能通过返回用户信息中的unionid来判断,而这个参数需要在微信开放平台上绑定相应微信公众号操作才会出现。
cs