zoukankan      html  css  js  c++  java
  • 夺命雷公狗---微信开发52----网页授权(oauth2.0)获取用户基本信息接口(2)

    我们在上一节课已经发送code给第三方了,那么这就要获取code去换取到用户的openid。

    第一步:编写create_baseurl.php(上一节课程已经写完了)

    第二步:编写vote1.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;
        
        
    ?>
    <!doctype html> 
    <html> 
        <head> 
            <meta charset="utf-8"> 
            <title>你最喜欢那个类型的女人</title> 
            <meta name="viewport" content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;"> 
            <meta name="apple-mobile-web-app-capable" content="yes"> 
            <meta name="apple-mobile-web-app-status-bar-style" content="black"> 
            <meta name="format-detection" content="telephone=no"> 
            <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> 
        </head> 
        <body> 
            <div data-role="page" id="page3"> 
                <div data-role="header"> 
                    <h1>你最喜欢那个类型的女人</h1> 
                </div> 
                <div data-role="content"> 
                    <form action="vote2.php" method="post" data-ajax="false" > 
                        <div data-role="fieldcontain"> 
                            <fieldset data-role="controlgroup"> 
                                <legend>你最喜欢那个类型的女人</legend> 
                                    <input type="radio" name="radio1" id="radio1_0" value="温柔" /> 
                                    <label for="radio1_0">温柔</label> 
                                    <input type="radio" name="radio1" id="radio1_1" value="开朗" /> 
                                    <label for="radio1_1">开朗</label>
                                    <input type="radio" name="radio1" id="radio1_2" value="拨辣" /> 
                                    <label for="radio1_2">拨辣</label> 
                                    <input type="radio" name="radio1" id="radio1_3" value="文静" /> 
                                    <label for="radio1_3">文静</label> 
                            </fieldset> 
                        </div>
                        <!--增加一个隐藏域,表示是那个用户提交的-->
                        <input type="hidden" name="openid" value="<?php echo $openid; ?>">
                    <div class="ui-block-a"> 
                        <button type="submit" data-role="button" >点击提交</button> 
                    </div> 
                </div> 
            </div> 
                </form> 
        </body> 
    </html> 

    第三步:编写vote2.php,主要用来处理用户提交的选择结果,并入库,比如我们这里面的“喜欢那个类型的女人”

    我们先来创建一张表,表名叫“lp_code”如下所示:

    然后我们创建一个vote2.php的文件,代码如下所示:

     <?php
        $openid = $_POST['openid'];
        $item = $_POST['radio1'];
        //因为取来的值后面自动加了一个 ' 所以要用substr去掉最后一个 '
        $openid = substr($openid,0,strlen($openid)-1); 
        
        $connect = 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);
        
     ?>
     <!doctype html> 
    <html> 
        <head> 
            <meta charset="utf-8"> 
            <title>数据采集系统</title> 
            <meta name="viewport" content="width=device-width,height=device-height,inital-scale=1.0, maximum-scale=1.0,user-scalable=no;"> 
            <meta name="apple-mobile-web-app-capable" content="yes"> 
            <meta name="apple-mobile-web-app-status-bar-style" content="black"> 
            <meta name="format-detection" content="telephone=no"> 
            <link href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" rel="stylesheet" type="text/css"> 
            <script src="http://code.jquery.com/jquery-1.6.4.min.js" type="text/javascript"></script> 
            <script src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js" type="text/javascript"></script> 
        </head> 
        <body> 
            <div data-role="page" id="page3"> 
                <div data-role="header"> 
                    <h1>水果投票情况一览</h1> 
                </div> 
                <div data-role="content"> 
                    <ul data-role="listview"> 
                        <?php
                            //这里遍历出来大家选择了什么,选了几遍
                            while($row = mysql_fetch_assoc($res){ ?>
                        <li>
                            喜欢<?php echo $row['item']; ?>
                            <span class="ui-li-count"><?php echo $row['counts']; ?></span>
                        </li>
                        <?php } ?>
                        <li><font color='red'><?php echo $response; ?></font></li>
                    </ul> 
                </div> 
            </div>
        </body> 
    </html> 

    然后反问上一节课里面的create_baseurl.php进行发送地址,当然如果在正常的情况下,

    我们都是通过以图文之类的形式发送的,如果直接推送的是一条链接,谁都不敢点嘛,嘻嘻

    然后在手机端里点击该链接进行投票即可

    温馨提示:

    code这里返回的access_token和基础接口里获取到的access_token是不一样的

  • 相关阅读:
    逻辑地址、线性地址、物理地址
    查找已知字符串子串
    替换字符串中的空格为%20
    资本的奥秘
    net::ERR_CONNECTION_RESET的处理方法
    SQL Server数据库从低版本向高版本复制数据库
    中式思维的五大逻辑缺陷(转)
    1年PK12年,中国式教育完败(转载)
    有关衣服的想法
    jquery邮箱自动补全
  • 原文地址:https://www.cnblogs.com/leigood/p/5256568.html
Copyright © 2011-2022 走看看