• 公众号第一天,认证与返回信息


    header('Content-type:text');
    define("TOKEN", "weixin");
    $wechatObj = new wechatCallbackapiTest();
    if (isset($_GET['echostr'])) {
        $wechatObj->valid();
    }else{
        $wechatObj->responseMsg();
    }
    
    class wechatCallbackapiTest
    {
        public function valid()
        {
            $echoStr = $_GET["echostr"];
            if($this->checkSignature()){
                header('content-type:text');
                echo $echoStr;
                exit;
            }
        }
    
        private function checkSignature()
        {
            $signature = $_GET["signature"];
            $timestamp = $_GET["timestamp"];
            $nonce = $_GET["nonce"];
    
            $token = TOKEN;
            $tmpArr = array($token, $timestamp, $nonce);
            sort($tmpArr, SORT_STRING);
            $tmpStr = implode( $tmpArr );
            $tmpStr = sha1( $tmpStr );
    
            if( $tmpStr == $signature ){
                return true;
            }else{
                return false;
            }
        }
    
        public function responseMsg()
        {
            $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
    
            if (!empty($postStr)){
                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                $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>";
                if($keyword == "yy")
                {
                    $msgType = "text";
                    $contentStr = date("Y-m-d H:i:s",time());
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    echo $resultStr;
                }
            }else{
               exit("");
            }
        }
    }
    ?>
  • 相关阅读:
    【Hive】HiveQL:数据定义
    【Hive】Hive安装与配置
    【Hadoop】Hadoop 2.7.6安装_伪分布式集群
    【RMAN】单实例环境RMAN备份和还原
    【SQL开发】Oracle 11g 分区技术
    【SQL开发】使用绑定变量 VS 不使用绑定变量
    MySQL索引最左前缀原则导致系统瘫痪
    阿里云RDS在线DDL工具gh-ost
    centos7下MongoDB3.4安装并解决告警
    ibtmp1文件过大
  • 原文地址:https://www.cnblogs.com/zywf/p/7944122.html
Copyright © 2020-2023  润新知