搜索
博主信息
博文 15
粉丝 2
评论 0
访问量 19250
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
微信登陆 (微信PC扫码授权登陆) 简单的php代码THINKPHP框架
LiuBo的博客
原创
2647人浏览过

微信PC扫码授权登陆 php简单示例代码
首先须要申请,必须企业,个体工商户,媒体等
申请地址 https://open.weixin.qq.com/

我直接上php最简单代码

开始登陆

//-------配置

$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);    




本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学