• 微信 图文消息


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

    ?>

  • 相关阅读:
    Visual Studio Ultimate 2012 RC win8 Metro应用的一个小bug样式继承
    windows 8 MSDN Examples 中一个奇怪的问题
    升级到IE9变成英文版的解决办法
    windows phone 中 TextBox 的诡异事件 TextChanged
    Windows Phone(OS7.1)中常用控件继承关系
    在Ubuntu使用gcc编译的程序无法运行的问题。
    Jquery实现简单图片切换
    div ul li 加a 错位问题
    asp.net mvc 2.0 数据验证
    asp.net mvc 2 (.net 4.0) “检测到有潜在危险的 Request.Form 值”的解决方法
  • 原文地址:https://www.cnblogs.com/simadongyang/p/8045171.html
Copyright © 2020-2023  润新知