PHP_微信token验证,单图文,多图文等一般操作

微信token验证

<?php
//define your token
define("TOKEN", "");
$wechatObj = new wechatCallbackapiTest();
$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"];

        //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);
                $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(!empty( $keyword ))
                {
                    $msgType = "text";
                    $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;
        }
    }
}

?>

单图文,多图文等一般操作

<?php
//define your token

define("TOKEN", "");
$wechatObj = new wechatCallbackapiTest();
$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"];

        //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);
            $MsgType= $postObj->MsgType;
            $j=$postObj->Location_X;
            $w=$postObj->Location_Y;

            $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($MsgType=="location"){
//              $url="http://api.map.baidu.com/telematics/v2/distance?waypoints=38.9214924386,121.5904756769;{$w},{$j}&ak=1a3cde429f38434f1811a75e1a90310c";
//              $fa=file_get_contents($url);
//              $f=simplexml_load_string($fa);
//              $juli=$f->results->distance;
//              $contentstring="你和我的距离有{$juli}米远"; 
//              $msgType = "text";     
//              $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentstring);
//              echo $resultStr;
//          }
            if($MsgType=="location"){
                 $textTpl = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                             <MsgType><![CDATA[news]]></MsgType>
                             <ArticleCount>1</ArticleCount>
                             <Articles>
                             <item>
                             <Title><![CDATA[本店位置]]></Title> 
                             <Description><![CDATA[按照地图标注来到本店]]></Description>
                             <PicUrl><![CDATA[%s]]></PicUrl>
                             <Url><![CDATA[%s]]></Url>
                             </item>
                             </Articles>
                             <FuncFlag>1</FuncFlag>
                            </xml>";   
                $url="http://api.map.baidu.com/staticimage?width=640&height=320&center=116.871729,38.264108&zoom=16&markers=116.871729,38.264108|116.870779,38.261797|116.87042,38.26004&markerStyles=l,M,0xFF0000|l,Y,0x008000";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,$url,$url);
                echo $resultStr;
            }

            //发送图片 回复文字         
            if ($MsgType=="image"){
                    $a=rand(1,3);
                     switch ($a)
                     {case "1";
                     $b="健康好人缘,婚姻幸福美满";
                     break;
                     case "2";
                     $b="贵人相助化险为夷的好运纹";
                     break;
                     default;
                     $b="才华横溢好研究,知识丰富事业有成";                             
                     }

                $msgType = "text";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $b);
                echo $resultStr;
            }   

            //关注部分
             $ev = $postObj->Event;
             if($ev == 'subscribe'){
                //$msgType = "text";
                //$contentStr = '欢迎关注王庆庆的微信测试平台';
                //$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                //echo $resultStr;
                $textTpl = "<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[news]]></MsgType>
                <ArticleCount>2</ArticleCount>
                <Articles>
                <item>
                <Title><![CDATA[欢迎关注]]></Title> 
                <Description><![CDATA[考试无处不在]]></Description>
                <PicUrl><![CDATA[http://wx.littlewindy.com/images/title_1.png]]></PicUrl>
                <Url><![CDATA[http://exam.littlewindy.com]]></Url>
                </item>

                <item>
                <Title><![CDATA[JQUI非常适应]]></Title>
                <PicUrl><![CDATA[http://wx.littlewindy.com/images/logo_1.png]]></PicUrl>
                <Url><![CDATA[http://jqui.littlewindy.com]]></Url>
                </item>
                </Articles>
                <FuncFlag>1</FuncFlag>
                </xml> ";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time);
                echo $resultStr;
            }



            if(!empty( $keyword )){
                if($keyword == '666'){
                    $contentStr = '<a href="http://wx.littlewindy.com/index.html">gameStart</a>';
                }elseif($keyword == '1'){
                    $textTpl = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[news]]></MsgType>
                        <ArticleCount>3</ArticleCount>
                        <Articles>
                        <item>
                        <Title><![CDATA[风儿笔记]]></Title> 
                        <Description><![CDATA[记一下又何妨]]></Description>
                        <PicUrl><![CDATA[http://wx.littlewindy.com/images/title_1.png]]></PicUrl>
                        <Url><![CDATA[http://name.littlewindy.com/index.php]]></Url>
                        </item>

                        <item>
                        <Title><![CDATA[分享]]></Title> 
                        <Description><![CDATA[考试无处不在]]></Description>
                        <PicUrl><![CDATA[http://wx.littlewindy.com/images/title_1.png]]></PicUrl>
                        <Url><![CDATA[http://name.littlewindy.com/index.php]]></Url>
                        </item>

                        <item>
                        <Title><![CDATA[JQUI非常适应]]></Title>
                        <PicUrl><![CDATA[http://wx.littlewindy.com/images/title_1.png]]></PicUrl>
                        <Url><![CDATA[http://name.littlewindy.com/index.php]]></Url>
                        </item>
                        </Articles>
                        <FuncFlag>1</FuncFlag>
                        </xml> 
                      ";
                      $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time);
                      echo $resultStr;
                }elseif($keyword == 'jqui'){
                    $contentStr = '<a href="http://jqui.littlewindy.com">JQUI</a>';
                }elseif($keyword == '2'){

                   $textTpl = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[music]]></MsgType>
                             <Music>
                             <Title><![CDATA[Alone题曲]]></Title>
                             <Description><![CDATA[XX演唱]]></Description>
                             <MusicUrl><![CDATA[http://wx.littlewindy.com/music/Alone.mp3]]></MusicUrl>
                             <HQMusicUrl><![CDATA[http://wx.littlewindy.com/music/Alone.mp3]]></HQMusicUrl>
                             </Music>
                            <FuncFlag>0</FuncFlag>
                            </xml>";           
                             $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time);
                             echo $resultStr;
                }
                else{
                    $contentStr = $this->getWeatcher($keyword);
                }
                $msgType = "text";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
            }else{
                echo '请输入1/2,系统会自动回复';
            }
        }else {
            echo '咋不说哈呢';
            exit;
        }
    }
    // 在微信后   台点击开始   微信向我的服务器发送GET(4个参数)请求,进行验证
    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;
        }
    }

}
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值