• 微信开发绑定事件实现机制


    define("TOKEN", "Your Token");
    
    $test = new TestController();
    $test->bind('subscribe');
    $test->trigger();
    
    
    
    class TestController{
        
        public $wechat = null;
        
        public function __construct(){
            
            $this->wechat = new Wechat();
    
        }
        
        
        public function bind($event, $method=''){
            $this->wechat->bind($event, $method);
        }
        
        public function trigger(){
            $this->wechat->trigger($this);
        }
        
        public function onSubscribe(){
            
                $request = $this->wechat->request;
                $fromUsername = $request->FromUserName;
                $toUsername = $request->ToUserName;
                $msgType = 'news';
                
                $xml = $this->wechat->setXmlHeader($fromUsername, $toUsername, $msgType);
                
                $content = '<ArticleCount>1</ArticleCount>
                                <Articles>
                                    <item>
                                        <Title><![CDATA[欢迎订阅]]></Title> 
                                        <Description><![CDATA[hehehe]]></Description>
                                        <PicUrl><![CDATA[http://pic.58pic.com/58pic/11/88/62/74h58PICNWd.jpg]]></PicUrl>
                                        <Url><![CDATA[http://www.baidu.com]]></Url>
                                    </item>
                                </Articles>'; 
                                
                $this->wechat->dispaly($xml, $content);
            
        }
        
        
        public function response(){
            
            $this->test();
        }
        
        
        public function test(){
            
            if($this->wechat->request->Content != ''){
                
                if(preg_match('/你是/', $this->wechat->request->Content)){
                    $this->wechat->p('我是你大哥呀!!!');
                }
                
                $this->wechat->p('您发送了一条消息');
            }
            
        }
        
    }
    
    
    
    
    
    
    
    class Wechat{
        
        public $event = '';
        public $bind = [];
        public $request = null;
    
        public function __construct(){
            
            if(isset($_GET['echostr'])) $this->valid();
            $this->request = $this->getRequest();
    
        }
        
        public function valid(){
            
            $echoStr = $_GET["echostr"];
            if($this->__checkSignature()){
                echo $echoStr; exit;
            }
        }
        
        public function bind($event, $method=''){
            
            if(empty($method)) $method = "on" . ucfirst($event);
            $this->bind[$event] = $method;
            
        }
        
        public function trigger($object){
            
            $method = isset($this->bind[$this->event])? $this->bind[$this->event] : 'response';
            call_user_func_array([$object, $method], []);
            
        }
    
        public function getRequest(){
            
            $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
            if(!empty($postStr)){
                    libxml_disable_entity_loader(true);
                      $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                    $postObj->Content = trim($postObj->Content);
                    $postObj->getTime = time();
                    if($postObj->MsgType == 'event') $this->event = (string) $postObj->Event;
                    
            }else{
                return null;
            }
            return $postObj;
            
        }
        
    
        
        public function setXmlHeader($fromUsername, $toUsername, $msgType){
            
            $xml = '<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[%s]]></MsgType>
                    {{__content__}}
                    </xml>';
                    
            $time = time();
            return sprintf($xml, $fromUsername, $toUsername, $time, $msgType);    
            
        }
        
    
        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;
            }
            
        }
        
        
        //调试方法
        public function p($content){
    
            $postObj = $this->request;
            $fromUsername = $postObj->FromUserName;
            $toUsername = $postObj->ToUserName;
            $textTpl = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                        <FuncFlag>0</FuncFlag>
                        </xml>";             
            $msgType = "text";
            if(!is_scalar($content)) $content = var_export($content, true);
            if(empty($content)) $content = 'empty';
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, $content);
            echo $resultStr;
            exit;        
            
        }
        
        public function dispaly($xml, $content){
            
            $xml = str_replace('{{__content__}}', $content, $xml);
            exit($xml);
        }
        
        
    }
  • 相关阅读:
    51Nod 1016 水仙花数 V2(组合数学,枚举打表法)
    April Fools Contest 2017 题解&源码(A,数学 B,数学 C,数学 D,字符串 E,数字逻辑 F,排序,卡时间,G,数学)
    统计0到n之间1的个数[数学,动态规划dp](经典,详解)
    hihoCoder #1082 : 然而沼跃鱼早就看穿了一切(字符串处理)
    洛谷 P1876 开灯(思维,枚举,规律题)
    Codeforces 789A Anastasia and pebbles(数学,思维题)
    51Nod 1182 完美字符串(字符串处理 贪心 Facebook Hacker Cup选拔)
    机器学习(Machine Learning)&深度学习(Deep Learning)资料
    看一下你在中国属于哪个阶层?
    python读取mnist
  • 原文地址:https://www.cnblogs.com/zbseoag/p/6138988.html
Copyright © 2020-2023  润新知