zoukankan      html  css  js  c++  java
  • 微信 图文消息

    <?php
    /**
      * wechat php test
      */

    //define your token
    define("TOKEN", "weixin");
    $wechatObj = new wechatCallbackapiTest();
    //$wechatObj->valid();
    $wechatObj->responseMsg();

    class wechatCallbackapiTest
    {
        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"];
            $postStr = file_get_contents("php://input");
            //file_put_contents("log.txt",$postStr,FILE_APPEND );
              //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 */
                    libxml_disable_entity_loader(true);
                      $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                    $fromUsername = $postObj->FromUserName;
                    $toUsername = $postObj->ToUserName;
                    $keyword = trim($postObj->Content);
                    //获取事件类型
                    $event = $postObj->Event;//subscribe unsubscribe
                    $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>";
                    switch($postObj->MsgType)
                    {
                        case 'event':
                        if($event == 'subscribe')
                        {
                            $contentStr = "杜丝莉 i love you";
                            $msgType = 'text';
                            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        echo $resultStr;

                        }
                        break;
                        case 'text':
                            if($keyword == 'sima')
                            {
                                $newTpl = "<xml>
                                <ToUserName><![CDATA[%s]]></ToUserName>
                                <FromUserName><![CDATA[%s]]></FromUserName>
                                <CreateTime>%s</CreateTime>
                                <MsgType><![CDATA[%s]]></MsgType>
                                <ArticleCount>2</ArticleCount>
                                <Articles>
                                <item>
                                <Title><![CDATA[%s]]></Title>
                                <Description><![CDATA[%s]]></Description>
                                <PicUrl><![CDATA[%s]]></PicUrl>
                                <Url><![CDATA[%s]]></Url>
                                </item>
                                <item>
                                <Title><![CDATA[%s]]></Title>
                                <Description><![CDATA[%s]]></Description>
                                <PicUrl><![CDATA[%s]]></PicUrl>
                                <Url><![CDATA[%s]]></Url>
                                </item>
                                </Articles>
                                </xml>";
                            $msgType = 'news';
                            $title1 = 'one';
                            $des1 = 'this is one';
                            $pic1 = 'http://www.simadongyang.com/wx/pic/one.jpg';
                            $url1 = 'www.baidu.com';

                            $title2 = 'two';
                            $des2 = 'this is two';
                            $pic2 = 'http://www.simadongyang.com/wx/pic/two.jpg';
                            $url2 = 'www.sougou.com';
                            $resultStr = sprintf($newTpl, $fromUsername, $toUsername, $time, $msgType, $title1,$des1,$pic1,$url1,$title2,$des2,$pic2,$url2);
                            echo $resultStr;
                            }
                            
                        break;
                    }             
                    /*if(!empty( $keyword ))
                    {
                          $msgType = "text";
                        if( $keyword == 'hello')
                        {
                            $contentStr = "你好";
                        }else{
                            $contentStr = "Welcome to wechat world!";
                        }
                        
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        echo $resultStr;
                    }else{
                        echo "Input something...";
                    }*/

            }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;
            }
        }
    }

    ?>

  • 相关阅读:
    【模版 Luogu P3808/P3796/P5357】AC自动机(简论)
    2019暑假 诸暨海亮集训游记
    【数据结构模版】可持久化线段树 && 主席树
    2019浙江集训——待整理的知识点、博客和题解
    浅谈树链剖分 F&Q
    Luogu P1967 NOIP2013 货车运输
    最小树形图(朱刘算法)--学习笔记
    Luogu P43916 图的遍历
    pytest扫盲12--assert断言
    pytest扫盲11--xfail参数详解
  • 原文地址:https://www.cnblogs.com/simadongyang/p/8045171.html
Copyright © 2011-2022 走看看