通过接入微信登录功能,用户可使用微信帐号快速登录你的网站,降低注册门槛,提高用户留存,示例代码如下:
第一步:请求CODE
$AppID='wx427bd690bdd58f';
$AppSecret='d4624c36b6795d1d99dcf0547af544';
$callback='https://www.z6.net.cn'; //回调地址
//微信登录
$state=md5(uniqid(rand(),TRUE)); //生成唯一随机串防CSRF攻击
$_SESSION["wx_state"]=$state; //存到SESSION
$callback=urlencode($callback);
$wxurl="https://open.weixin.qq.com/connect/oauth2/authorize?appid={$AppID}&redirect_uri={$callback}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect";
header("Location: $wxurl");
第二步:通过code
获取access_token
//配置
$AppID='wx427bd690bdd58f';
$AppSecret='d4624c36b6795d1d99dcf0547af544';
//得到 access_token 与 openid
$url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$AppID.'&secret='.$AppSecret.'&code='.$_GET['code'].
'&grant_type=authorization_code';
$ch=curl_init();
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch,CURLOPT_URL,$url);
$json=curl_exec($ch);
curl_close($ch);
$arr=json_decode($json,1);
第三步:通过access_token
调用接口
//得到用户资料
$url='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
$ch=curl_init();
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch,CURLOPT_URL,$url);
$json=curl_exec($ch);
curl_close($ch);
$arr=json_decode($json,1);
未经允许不得转载:任鹏个人博客 » 网站微信授权登录详细教程
最新评论
Forex wiki. https://lt.forex-stock-bitcoin-brokers.com
Magnificent items from you, man. I have take note your stuff
Following on from the 3rd March Meetings held by economic de
It is remarkable, rather valuable message dfgdlfg2131.32
一般都会有一个沙盒期的,过了沙盒期就会慢慢放出来
百度不收录是应为是新站的原因吗?
The spike in consumer prices that left inflation at a four-d