zoukankan      html  css  js  c++  java
  • 夺命雷公狗---微信开发44----用户分组管理接口(实例)

    我们来做一个案例,我们先生成两个永久的二维码,

    场景分别是1001和1002

    当用户是通过关注1001二维码扫描进行关注的移动到id为100的(php小组)进行管理

    当用户是通过关注1002二维码扫描进行关注的移动到id为102的(asp小组)进行管理

    目前我们所拥有的分组如下图所示:

    我们先在index.php里面进行加工,代码如下所示:

    <?php
    /**
      * wechat php test
      */
    
    //define your token
    require_once "common.php";
    //这里是引入curl发送函数的类
    require_once 'WeChat.class.php';
    define("TOKEN", "twgdh");
    
    //这里让这个类继承了curl发送参数的类
    class wechatCallbackapiTest extends WeChat
    {
        public function valid()
        {
            $echoStr = $_GET["echostr"];
    
            //valid signature , option
            if($this->checkSignature()){
                echo $echoStr;
                exit;
            }
        }
    
        public function responseMsg()
        {
            //get post data, May be due to the different environments
            $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
            
              //extract post data
            //extract post data
            if (!empty($postStr)){
                    /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                       the best way is to check the validity of xml by yourself */
                    // 使用simplexml技术对xml进行解析 
                    // libxml_disable_entity_loader(true), 是从安全性考虑,为了防止xml外部注入,
                    //只对xml内部实体内容进行解析
                    libxml_disable_entity_loader(true);
                    //加载 postStr 字符串
                      $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);            
                    $fromUsername = $postObj->FromUserName;
                    $toUsername = $postObj->ToUserName;
                    $keyword = trim($postObj->Content);
                    $time = time();
                    global $tmp_arr;
                    
                    //根据接收到的消息类型,来进行分支处理(switch)
                    switch($postObj->MsgType)
                    {
                        case 'event':
                            //用户未关注时,进行关注后的事件推送
                            if($postObj->Event == 'subscribe')
                            {
                                //获取用户的EventKey,根据不同的情况,将用户移动到对应组
                                $EventKey = $postObj->EventKey;
                                if($EventKey == 'qrscene_1001')
                                {
                                    $groupid = '100';
                                }else if($EventKey == 'qrscene_1002')
                                {
                                    $groupid = '102';
                                }
                                //把该用户移动对对应组
                                require_once 'get_token.php';
                                $url = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token={$access_token}";
                                $post ='{"openid":"'.$fromUsername.'","to_groupid":'.$groupid.'}';
                                $res = $this->http_request($url, $post);
                                //转换下名称,到时候发给客户端的
                                $gid = '';
                                if($groupid == '100'){
                                    $gid = 'PHP技术小组';
                                }else if($groupid == '102'){
                                    $gid = 'ASP技术小组';
                                }
                                $contentStr = "夺命雷公狗欢迎您!, 您已经加入到 {$gid}";
                                $resultStr = sprintf($tmp_arr['text'], $fromUsername, $toUsername, $time, $contentStr);
                                echo $resultStr; 
                            }
                            break;
                        case 'text':
                            //回复文本模板
                            if(!empty($keyword))
                            {
    
                                $contentStr = "夺命雷公狗平台已经收到您的问题, 稍后回复您!";
                                $resultStr = sprintf($tmp_arr['text'], $fromUsername, $toUsername, $time, $contentStr);
                                echo $resultStr;
                                
                            }
                            else{
                                $contentStr = "您输入的格式不正确";
                                $resultStr = sprintf($tmp_arr['text'], $fromUsername, $toUsername, $time, $contentStr);
                                echo $resultStr;
                            }
                            break;
                        //default:
                    }
            }else {
                echo "";
                exit;
            }
        }
            
        private function checkSignature()
        {
            // you must define TOKEN by yourself
            if (!defined("TOKEN")) {
                throw new Exception('TOKEN is not defined!');
            }
            
            $signature = $_GET["signature"];
            $timestamp = $_GET["timestamp"];
            $nonce = $_GET["nonce"];
                    
            $token = TOKEN;
            $tmpArr = array($token, $timestamp, $nonce);
            // use SORT_STRING rule
            sort($tmpArr, SORT_STRING);
            $tmpStr = implode( $tmpArr );
            $tmpStr = sha1( $tmpStr );
            
            if( $tmpStr == $signature ){
                return true;
            }else{
                return false;
            }
        }
    }
    
    //如果这段代码放在上面,那程序将会报错,因为继承的问题,会显示类没有找到
    $wechatObj = new wechatCallbackapiTest();
    //当接入成功后,请注销这句话,否则,会反复验证。
    //$wechatObj->valid();
    //添加响应请求的语句
    $wechatObj->responseMsg();
    
    ?>

    核心代码如下:

     

    然后进行访问forever_qrcode.php进行反问

    见到这里恭喜您,您离成功又进了一步,下一步当然是用手机对她进行扫描啦,然后访问query_group.php文件即可确定用户已经百分百加入PHP小组了

    那么恭喜您,已经成功了加入了,嘻嘻

  • 相关阅读:
    Object Files (.obj)
    使用IIS Microsoft的web服务器和ftp服务器
    关于matlab
    char, signed char, unsigned char的区别
    百度面试
    google笔试题_2011
    ioctl 函数与网络接口
    Unity之热更新:(三)XLua
    C#之设计模式:观察者模式
    C#:委托
  • 原文地址:https://www.cnblogs.com/leigood/p/5244053.html
Copyright © 2011-2022 走看看