微信公众号开发:用户授权获取用户信息

news/2025/3/15 11:14:34/


获取用户授权用户信息中的assesstoken调用不限次数!!!有效期两小时

header("Content-type: text/html; charset=utf-8"); 
$openid='';
$username='';
$img='';
if($_COOKIE['<span style="font-family: Arial, Helvetica, sans-serif;">openid</span><span style="font-family: Arial, Helvetica, sans-serif;">']){</span>
$openid=$_COOKIE['<span style="font-family: Arial, Helvetica, sans-serif;">openid</span><span style="font-family: Arial, Helvetica, sans-serif;">'];</span>
$username=$_COOKIE['username'];
$img=$_COOKIE['img'];
}else{
$code = $_GET['code'];
$state = $_GET['state'];
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=公众号id&secret=公众号秘钥&code=' . $code . '&grant_type=authorization_code';
$result = null;
try {$result = curlGet($url);$obj = json_decode($result);$getInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $obj->access_token . "&openid=" . $obj->openid;//微信返回值$userObj = json_decode(curlGet($getInfoUrl));
} catch (Exception $e) {echo $e->getTraceAsString();}
//echo $userObj->openid;die;
//echo $userObj->openid;die;
$openid=$userObj->openid;
$username=urlencode(str_replace(array("'", "\\"), array(''), $userObj->nickname));
$img=$userObj->headimgurl;
setcookie('uid',$userObj->openid,time()+3*24*60*60);
setcookie('username',$username,time()+3*24*60*60);
setcookie('img',$img,time()+3*24*60*60);
}function curlGet($url, $method = 'get', $data = '')
{$ch = curl_init();$header = "Accept-Charset: utf-8";curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);curl_setopt($ch, CURLOPT_AUTOREFERER, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $data);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);$temp = curl_exec($ch);return $temp;
}



http://www.ppmy.cn/news/813746.html

相关文章

缓存穿透与布隆过滤器(Bloom Filter)

缓存穿透 在高并发场景中&#xff0c;为了避免请求直接打到数据库上&#xff08;会导致数据库出现性能问题&#xff0c;从而影响整个系统&#xff09;&#xff0c;常使用缓存来处理大部分请求&#xff0c;如memcached、ehcache、redis等。 但对于一些恶意请求&#xff0c;传统…

我的世界服务器皮肤怎么用文件夹,我的世界皮肤正文件,皮肤制作器怎么打开文件夹...

打开versions&#xff0c;我的世界皮肤站怎么找皮肤文件夹里百面有个小茶壶形状的文件&#xff0c;用压度缩工具打开它&#xff0c;依次打知开assets&#xff0c;minecraft&#xff0c;&#xff0c;textures&#xff0c;entity&#xff0c;先将里面自带道的原皮肤删掉&#xff…

微信小程序实现循环列表中加样式

最近开始接触小程序&#xff0c;所以记录一下做项目过程中遇到的问题&#xff0c;由于小程序不想做网站开发&#xff0c;可以直接点击添加样式&#xff0c;得通过this.setdata来设置 这是效果图&#xff1a; 第一步&#xff1a;核心是判断当前点击的项的id&#xff0c;通过三目…

微信公众号全局返回码

公众号每次调用接口时&#xff0c;可能获得正确或错误的返回码&#xff0c;开发者可以根据返回码信息调试接口&#xff0c;排查错误。 全局返回码说明如下&#xff1a; 返回码 说明 -1 系统繁忙&#xff0c;此时请开发者稍候再试 0 请求成功 40001 获取 access_token 时 A…

LeetCode 146. LRU缓存机制Golang版

LeetCode 146. LRU缓存机制Golang版 1. 问题描述 运用你所掌握的数据结构&#xff0c;设计和实现一个 LRU (最近最少使用) 缓存机制 。 实现 LRUCache 类&#xff1a; LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存 int get(int key) 如果关键字 key 存…

使用微信公众号回复带有外链的文字

后台发送 使用客服消息回复文字&#xff0c;按照以下格式&#xff1a; 您好&#xff0c;< a href’https://www.baidu.com‘>点我进行页面跳转

我的世界服务器皮肤怎么用文件夹,我的世界怎么用皮肤文件,怎么通过文件夹更改皮肤...

打开versions&#xff0c;我的世界怎么用文件换皮肤教程里百面有个小茶壶形状的文件&#xff0c;用压度缩工具打开它&#xff0c;依次打知开assets&#xff0c;minecraft&#xff0c;&#xff0c;textures&#xff0c;entity&#xff0c;先将里面自带道的原皮肤删掉&#xff0c…

ios在微信中遇见的bug

ios在微信中目前遇见的bug 1.ios在微信浏览器中不能获取文本域的焦点 将有这几个属性删除即可 * {-webkit-touch-callout:none;-webkit-user-select:none;-khtml-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none; } input {-webkit-user-sel…