php pc机微信扫码,ThinkPHP 微信登陆 (微信PC扫码授权登陆) php 最简单代码

ThinkPHP 微信登陆 (微信PC扫码授权登陆) php 最简单代码本主题由 隐藏人物 创建于 2016-3-28 0:15:28

http://www.thinkphp.cn/code/1556.html

开始登录

//-------配置

$AppID = 'wx033336c794d4';

$AppSecret = 'd4624c36333337af5443d';

$callback = 'http://www.yun8888.net/weixin/'; //回调地址

//微信登录

session_start();

//-------生成唯一随机串防CSRF攻击

$state = md5(uniqid(rand(), TRUE));

$_SESSION["wx_state"] = $state; //存到SESSION

$callback = urlencode($this->callback);

$wxurl = "https://open.weixin.qq.com/connect/qrconnect?appid=".$this->AppID."&redirect_uri={$callback}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect";

header("Location: $wxurl");

回调地址

if($_GET['state']!=$_SESSION["wx_state"]){

exit("5001");

}

$AppID = 'wx33333333334d4';

$AppSecret = 'd4624c363333330547af5443d';

$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 与 openid

print_r($arr);

$url='https://api.weixin.qq.com/sns/userinfo?access_token='.$arr['access_token'].'&openid='.$arr['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);

得到 用户资料

print_r($arr);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值