微信PC扫码授权登陆 php简单示例代码
首先须要申请,必须企业,个体工商户,媒体等
申请地址 https://open.weixin.qq.com/
我直接上php最简单代码
开始登陆//-------配置<br>
$AppID = 'wx033336c794d4';<br>
$AppSecret = 'd4624c36333337af5443d';<br>
$callback = 'http://www.yun8888.net/weixin/'; //回调地址<br>
<br>
//微信登录<br>
session_start();<br>
//-------生成唯一随机串防CSRF攻击<br>
$state = md5(uniqid(rand(), TRUE));<br>
$_SESSION["wx_state"] = $state; //存到SESSION<br>
$callback = urlencode($this->callback);<br>
$wxurl = "https://open.weixin.qq.com/connect/qrconnect?appid=".$this->AppID."&redirect_uri={$callback}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect";<br>
header("Location: $wxurl");
回调地址if($_GET['state']!=$_SESSION["wx_state"]){<br>
exit("5001");<br>
}<br>
$AppID = 'wx33333333334d4';<br>
$AppSecret = 'd4624c363333330547af5443d';<br>
$url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$AppID.'&secret='.$AppSecret.'&code='.$_GET['code'].'&grant_type=authorization_code';<br>
<br>
$ch = curl_init();<br>
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);<br>
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);<br>
curl_setopt($ch, CURLOPT_URL, $url);<br>
$json = curl_exec($ch);<br>
curl_close($ch);<br>
<br>
$arr=json_decode($json,1);<br>
<br>
//得到 access_token 与 openid<br>
print_r($arr); <br>
<br>
$url='https://api.weixin.qq.com/sns/userinfo?access_token='.$arr['access_token'].'&openid='.$arr['openid'].'&lang=zh_CN';<br>
$ch = curl_init();<br>
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);<br>
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);<br>
curl_setopt($ch, CURLOPT_URL, $url);<br>
$json = curl_exec($ch);<br>
curl_close($ch);<br>
$arr=json_decode($json,1);<br>
得到 用户资料<br>
print_r($arr);
最终输出如图所示
//----------------------------------------------------------<br>
//声明: 本代码并非完美,也许存在不佳之处,请放肆吐槽!<br>
//作者: 小曾<br>
//扣扣: 839024615<br>
//网址: www.yun8888.net<br>
//交流群 324098841<br>
//关于我的ThinkPHP http://www.thinkphp.cn/u/87696.html<br>
//----------------------------------------------------------
AD:真正免费,域名+虚机+企业邮箱=0元