刚才的数据都还没入库,所以现在我们开始将数据入库,首先我们创建一张“userinfo”的数据表,如下所示:
我们来对get_userinfo.php来进行加工一下呗,嘻嘻,代码如下:
<?php header("Content-Type:text/html;charset=utf-8"); require_once "get_token.php"; require_once "common.php"; //获取指定用户的openid也就是主文件里面的$fromUsername $fromUsername = "oB1_6tzeHj-KG7kL8Thmu6VcF2BM"; $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$access_token}&openid={$fromUsername}"; //发送请求 $res = http_request($url,null); $res = json_decode($res); //获取我们所需要的各种信息 $nickname = $res->nickname;//用户昵称 $sex = $res->sex;//性别 $country = $res->country;//国家 $province = $res->province;//省份 $city = $res->city;//城市 $address = "{$country}{$province}{$city}"; $subscribe_time = $res->subscribe_time;//用户关注的时间 $sjc = date(‘Y-m-d-H:i:s‘); $groupid = $res->groupid;//用户所在的分组ID $headimgurl = $res->headimgurl;//用户头像 //利用substr函数获取小头像地址 0代表从第0个开始获取,-1代表去掉最后一个 $samll_head_imgurl = substr($headimgurl,0,-1).‘132‘; //设置一个空性别 $sexs = ‘‘; if($sex == ‘1‘){ $sexs = ‘男‘; }else if($sex == ‘2‘){ $sexs = ‘女‘; }else{ $sexs = ‘他不说,估计是人妖‘; } echo "<h1>报告老大您要追查的通缉犯</h1><br />"; echo "<table border=‘1‘>"; echo "<tr> <th>昵称</th> <th>性别</th> <th>地址</th> <th>观众时间</th> <th>小头像</th> </tr>"; echo " <tr> <td>{$nickname}</td> <td>{$sexs}</td> <td>{$address}</td> <td>{$sjc}</td> <td><img src=‘".$samll_head_imgurl."‘></td> </tr> "; echo "</table>"; echo "<br />"; echo "<br />"; echo "<br />"; $connect = mysql_connect(‘localhost‘,‘root‘,‘root‘) or die(‘数据库连接失败‘); mysql_select_db(‘wxdb‘,$connect); mysql_query(‘set names utf8‘); $sql = "insert into userinfo (id,openid,nickname,address,zctime,headimgurl,sex,mes) values(null,‘{$fromUsername}‘,‘{$nickname}‘,‘{$address}‘,‘{$subscribe_time}‘,‘{$samll_head_imgurl}‘,‘{$sex}‘,‘留条言呗!!!‘)"; mysql_query($sql); echo "保存成功";
然后再访问下get_userinfo.php,看到下面的保存成功,心里有点小激动,
然后我们再进数据库看下是否成功,
YES果然入库了,这只是在测试而已,如果是真的是项目中的那么我们往往是要写到index.php主文件里面的
case ‘event‘: //用户未关注时,进行关注后的事件推送 if($postObj->Event == ‘subscribe‘) { }
应该都是关注后直接入库。。。
现在能将突然存进数据库里面了,那么我们再来写个show_users.php的文件来让他进行遍历出来呗,我们用第公众平台营销咨询系统,也就35课里面的代码来套下即可,代码如下所示:
<?php //链接数据库 $connect = mysql_connect(‘localhost‘,‘root‘,‘root‘); mysql_select_db(‘wxdb‘,$connect); mysql_query(‘set names utf8‘); //分页开工咯,先取个别名counts $sql = "select count(*) as counts from userinfo"; $res = mysql_query($sql); $row = mysql_fetch_assoc($res); //这里的$row_num是总的咨询条数 $row_num = $row[‘counts‘]; //我们规定每页显示5条记录 $page_size = 5; //计算总页数 $page_count = ceil($row_num/$page_size); //接收当前用户点击的是第几页 $page_num = $_GET[‘page_num‘]; //判断$page_num是否为空,如果是空的默认给他一个1 if(empty($page_num)){ $page_num = 1; } //计算从那条记录开始获取 $begin = ($page_num-1)*$page_size; $sql = "select * from userinfo order by id desc limit {$begin},{$page_size}"; $info_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="./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‘> <ul data-role=‘listview‘> <?php $row = array(); while($row = mysql_fetch_assoc($info_res)){ ?> <li height=‘200px‘> 昵称:<?php echo $row[‘nickname‘]; ?><br /> 地址:<?php echo $row[‘address‘]; ?> <p class=‘ui-li-aside‘><img width=‘40px‘ src=‘<?php echo $row[‘headimgurl‘]; ?>‘></p> </li> <?php } ?> </ul> </div> <!--分页的按钮开始--> <div data-role="controlgroup" data-type="horizontal"> <?php if($page_num!=1 && $page_num>1){ ?> <a href="/show_users.php?page_num=<?php echo $page_num-1; ?>" data-role="button">上一页</a> <?php } ?> <?php if($page_count > $page_num){ ?> <a href="/show_users.php?page_num=<?php echo $page_num+1; ?>" data-role="button">下一页</a> <?php } ?> </div> <!--分页的按钮结束--> </div> </body> </html>
然后对对她进行访问即可即可,如下图所示: