zoukankan      html  css  js  c++  java
  • 公众号第一天,认证与返回信息

    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("");
            }
        }
    }
    ?>
  • 相关阅读:
    党务
    平台 大赛 公司
    音乐
    有趣的博主
    C++获取命令行参数命令
    360读全景
    3 海康网络相机官方例程(3)OpenCv + ffmpeg + rtmp 实现摄像头采集数据直播功能(不带cuda加速)
    使用opencv4进行分类器训练
    经典环境(2)OpenCV412+OpenCV-Contrib +vs2015+cuda10.1编译
    经典环境(1)OpenCV3.4.9+OpenCV-Contrib +vs2015+cuda10.1编译
  • 原文地址:https://www.cnblogs.com/zywf/p/7944122.html
Copyright © 2011-2022 走看看