白羽
2019-01-08
来源 :网络
阅读 983
评论 0
摘要:本文将带你了解微信开发52----网页授权(oauth2.0)获取用户基本信息接口(2),希望本文对大家学微信有所帮助。
本文将带你了解微信开发52----网页授权(oauth2.0)获取用户基本信息接口(2),希望本文对大家学微信有所帮助。
我们在上一节课已经发送code给第三方了,那么这就要获取code去换取到用户的openid。
第一步:编写create_baseurl.php(上一节课程已经写完了)
<!--?php require_once "common.php"; //获取code $code = $_GET['code']; //公众号的appid $appid = "wxed89d8f74fa6fc51"; //公众号的appsecret $appsecret = 'd4624c36b6795d1d99dcf0547af5443d'; $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$appsecret}&code={$code}&grant_type=authorization_code"; //发出请求 $res = http_request($url,null); //json解码 $res = json_decode($res); //获取openid $openid = $res ---> openid; ?> </pre><meta charset="utf-8" /><title></title><meta content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;" name="viewport" /><meta content="yes" name="apple-mobile-web-app-capable" /><meta content="black" name="apple-mobile-web-app-status-bar-style" /><meta content="telephone=no" name="format-detection" /><link href="./jquery.mobile-1.0.min.css" rel="stylesheet" type="text/css" /><script src="./jquery-1.6.4.min.js" type="text/javascript"></script><script src="./jquery.mobile-1.0.min.js" type="text/javascript"></script>
第二步:编写vote1.php(上一节课已经谢了一大部分,现在我们来完成他)
你最喜欢那个类型的女人
<div data-role="content"><form action="vote2.php" data-ajax="false" method="post"> <div data-role="fieldcontain"> <fieldset data-role="controlgroup"> <legend>你最喜欢那个类型的女人</legend> <input id="radio1_0" name="radio1" type="radio" value="温柔" /> <label for="radio1_0">温柔</label> <input id="radio1_1" name="radio1" type="radio" value="开朗" /> <label for="radio1_1">开朗</label> <input id="radio1_2" name="radio1" type="radio" value="拨辣" /> <label for="radio1_2">拨辣</label> <input id="radio1_3" name="radio1" type="radio" value="文静" /> <label for="radio1_3">文静</label></fieldset> </div> <!--增加一个隐藏域,表示是那个用户提交的--><input name="openid" type="hidden" value="<?php echo $openid; ?>" /> <div><button data-role="button" type="submit">点击提交</button></div></form>
第三步:编写vote2.php,主要用来处理用户提交的选择结果,并入库,比如我们这里面的“喜欢那个类型的女人”
我们先来创建一张表,表名叫“lp_code”如下所示:
然后我们创建一个vote2.php的文件,代码如下所示:
mysql_connect('localhost','root','root') or die('数据库链接失败'); mysql_select_db('wxdb',$connect); mysql_query('set names utf8'); $sql = "select openid from lp_code where openid='{$openid}'"; $res = mysql_query($sql); //$response主要用来做提示用的 $response = ''; //这里主要作用是判断一下如果投过票的就不让他再投了 if($row = mysql_fetch_assoc($res)){ $response = "您已选择过噢,本轮活动只能选择一次"; }else{ $sql = "insert into lp_code (id,openid,item) values(NULL,'{$openid}','{$item}')"; mysql_query($sql); $response = "恭喜您,您已经选择成功"; } //同分组的形式统计一下各个水果投票的次数 $sql = "select item, count(*) as counts from lp_code group by item"; $res = mysql_query($sql); ?-->
水果投票情况一览
喜欢
然后反问上一节课里面的create_baseurl.php进行发送地址,当然如果在正常的情况下,
我们都是通过以图文之类的形式发送的,如果直接推送的是一条链接,谁都不敢点嘛,嘻嘻
然后在手机端里点击该链接进行投票即可
温馨提示:
code这里返回的access_token和基础接口里获取到的access_token是不一样的
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之微信频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号