Web全栈笔记之微信公众平台开发
2016-11-28 08:56
295 查看
1.微信公众测试平台
用微信扫一扫登陆微信公众测试平台2.接口配置
接入指南查看接口文档
下载示例代码
示例代码文件设置
然后上传到服务器
公众平台设置接口信息
如果填写信息无误提示配置成功
3.获取access_token
获取access_token文档access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。开发者需要进行妥善保存。access_token的存储至少要保留512个字符空间。access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效。
请求地址 2小时期限 每天获取次数有限 需要保存下来,过期再重新获取
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
access_token.txt文件
access_token:保存获取到的access_token值,time:记录获取时间{"access_token":"","time":0}
<?php $appid = 'wx3ccefa9090f7d87 4000 2'; $secret = 'b6c7ad6f13d8755953afd709365eab1a'; //获取access_token地址 $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret; //先查看access_token.txt里是否有access_token $str = file_get_contents("access_token.txt"); $data = json_decode($str); //判断是否过期,如果是重新获取access_token值 if ($data->time<time()) { // 获取access_token地址的内容 $str = file_get_contents($url); // 把获取到的JSON字符串转为json对象 $json = json_decode($str); //记录获取到的access_token和时间 $data->access_token = $json->access_token; $data->time = time()+7000; $str = json_encode($data); //把获取到access_token值保存到access_token.txt file_put_contents("access_token.txt", $str); }
4.获取用户信息
获取用户信息文档<?php //用户的openid $openid = "o-6mGw3buyMYKQz15gQmKeY4KFEg"; // //获取用户信息地址 $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$data->access_token.'&openid='.$openid.'&lang=zh_CN'; //获取接口信息 $str = file_get_contents($url); //把获取到的内容转为json对象 $json = json_decode($str); $sex = $json->sex==1?"男":"女"; echo "<table>"; echo "<tr> <td>头像<td> <td>姓名<td> <td>性别<td> <td>城市<td> </tr>"; echo "<tr> <td><img style='width:100px' src='{$json->headimgurl}'><td> <td>{$json->nickname}<td> <td>{$sex}<td> <td>{$json->city}<td> </tr>"; echo "</table>"; ?>
5.获取用户列表
获取用户列表文档// 获取用户列表 $url = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token='.$data->access_token; $str = file_get_contents($url); $json2 = json_decode($str); // echo "$json->count"; echo "<table>"; echo "<tr> <td>头像<td> <td>姓名<td> <td>性别<td> <td>城市<td> </tr>"; for ($i=0; $i < $json2->count; $i++) { // echo $json->data->openid[$i]."<br>"; $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$data->access_token.'&openid='.$json2->data->openid[$i].'&lang=zh_CN'; $str = file_get_contents($url); $json = json_decode($str); $sex = $json->sex==1?"男":"女"; echo "<tr> <td><img style='width:100px' src='{$json->headimgurl}'><td> <td>{$json->nickname}<td> <td>{$sex}<td> <td>{$json->city}<td> </tr>"; } echo "</table>";
6.用户授权
用户授权文档<?php echo "授权页面"; $code = $_GET["code"];//用户确认授权后返回过来的code值 $appid = 'wx3ccefa9090f7d872'; $secret = 'b6c7ad6f13d8755953afd709365eab1a'; //通过code值获取access_token的地址 $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code'; $str = file_get_contents($url); $json = json_decode($str);//把获取到的内容转为json对象 //用户接口地址 $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$json->access_token.'&openid='.$json->openid.'&lang=zh_CN'; //获取用户接口信息 $str = file_get_contents($url); //把获取到的内容转为json对象 $json = json_decode($str); $sex = $json->sex==1?"男":"女"; echo "<table>"; echo "<tr> <td>头像<td> <td>姓名<td> <td>性别<td> <td>城市<td> </tr>"; echo "<tr> <td><img style='width:100px' src='{$json->headimgurl}'><td> <td>{$json->nickname}<td> <td>{$sex}<td> <td>{$json->city}<td> </tr>"; echo "</table>"; // echo urlencode('http://weixin5.applinzi.com/code.php'); // https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=http%3A%2F%2Fweixin5.applinzi.com%2Fcode.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect ?>
7.微信JS-SDK
微信JS-SDK文档页面代码需要设置APPIP APPSECRET
相关文章推荐
- WEB开发开篇【treeview开发笔记】
- ASP.NET控件开发学习笔记--第7回 从WebControl继承
- SharePoint开发学习笔记3——Visual Web Part及自定义配置界面
- Android开发学习笔记:浅谈WebView
- 步步为营 SharePoint 开发学习笔记系列 十、SharePoint web service 开发(下)
- Log4j在Java Web开发中的使用笔记
- [Web开发笔记][2]Servlet配置的一点经验
- Java Web开发学习笔记之二
- Web开发笔记2——MySQL拾零
- Web前端开发修炼之道—笔记
- ASP.NET控件开发学习笔记--第8回 WebControl分析
- Python Web 开发 学习笔记1 安装配置 python+django
- soap web服务开发笔记
- VS2008 Web开发笔记 - Ajax 客户端编程 (一)
- 笔记之 02_传智播客AJAX视频教程_使用IntelliJ开发Web项目
- Web Services + Click Once开发笔记
- web开发敏捷之道2nd-rails进行web开发-笔记(全)
- JSP学习笔记(二)Web开发基础
- asp.net笔记----Web开发的一些基本原则和一些对象方法,笔记有点乱!!
- 宋利兴_笔记_Java Web应用程序开发虚拟目录的根目录