码迷,mamicode.com
首页 > 微信 > 详细

微信验证及自动回复

时间:2016-05-12 18:15:22      阅读:394      评论:0      收藏:0      [点我收藏+]

标签:

上传到服务器,微信验证(wx_sample.php)

说明

第一次验证,调用valid方法,之后就可以切换成responseMsg方法进行自动回复了

判断事件类型:text为发送文本事件,event为触发事件

<?php
//定义你的验证token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();

class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature()
    {
        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;
        }
    }

    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
            $MsgType = trim($postObj->MsgType);
            //判断是发送文本事件还是触发事件
            switch ($MsgType)
            {
                case "text":
                    $resultStr = $this->receiveText($postObj);
                    break;
                case "event":
                    $resultStr = $this->receiveEvent($postObj);
                    break;
                default:
                    $resultStr = "";
                    break;
            }
            echo $resultStr;
        }else {
            echo "";
            exit;
        }
    }

    private function receiveText($object)
    {
        $funcFlag = 0;
        $key=$object->Content;
        if($key==‘房客‘){
            $contentStr[] = array("Title" =>"房客指南",
            "Description" =>"查看恋家房客指南",
            "PicUrl" =>"http://www.test.com/img/1.jpg",
            "Url" =>"http://www.test.com/tg.php");
            $resultStr = $this->transmitNews($object, $contentStr);
        }else{
            $key=intval($key);
            if($key){
                $contentStr[] = array("Title" =>‘推送框标题‘,
                "Description" =>‘推送框说明‘,
                "PicUrl" =>‘http://www.test.com/img/‘.$key.‘.jpg‘,//显示图片
                "Url" =>"http://www.test.com/room/$key");//点击后跳转页面
                $resultStr = $this->transmitNews($object, $contentStr);
            }else{
                //返回文本
                $contentStr = "请输入正确内容(数字)";
                $resultStr = $this->transmitText($object, $contentStr, $funcFlag);
            }
        }
        return $resultStr;
    }

    private function receiveEvent($object)
    {
        $contentStr = "";
        switch ($object->Event)
        {
        //关注微信事件
            case "subscribe":
                $contentStr = "欢迎关注恋家网";
        //取消关注事件
            case "unsubscribe":
                break;
        //点击事件
            case "CLICK":
                switch ($object->EventKey)
                {
                    //如果点击按钮接收关键词为mtr
                    case "mtr":
                        $contentStr[] = array("Title" =>"神秘房客报名", 
                        "Description" =>"恋家神秘房客报名享大礼", 
                        "PicUrl" =>"http://www.test.com/img/mtr.jpg", 
                        "Url" =>"http://www.test.com/mtr.php");
                        break;
                    //如果点击按钮接收关键词为lg
                    case "lg":
                        $contentStr[] = array("Title" =>"房东指南", 
                        "Description" =>"查看恋家房东指南", 
                        "PicUrl" =>"http://www.test.com/img/lg.jpg", 
                        "Url" =>"http://www.test.com/lg.php");
                        break;
                    //如果点击按钮接收关键词为tg
                    case "tg":
                        $contentStr[] = array("Title" =>"房客指南", 
                        "Description" =>"查看恋家房客指南", 
                        "PicUrl" =>"http://www.test.com/img/tg.jpg", 
                        "Url" =>"http://www.test.com/tg.php");
                        break;
                    //其他点击事件
                    default:
                        $contentStr[] = array("Title" =>"默认菜单回复", 
                        "Description" =>"您正在使用的是恋家网的自定义菜单测试接口", 
                        "PicUrl" =>"http://www.test.com/img/default.jpg", 
                        "Url" =>"http://www.test.com/img/default.php");
                        break;
                }
                break;
            //其它事件
            default:
                break;
        }
        if (is_array($contentStr)){
            $resultStr = $this->transmitNews($object, $contentStr);
        }else{
            $resultStr = $this->transmitText($object, $contentStr);
        }
        return $resultStr;
    }

    private function transmitText($object, $content, $funcFlag = 0)
    {
        $textTpl = "<xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[text]]></MsgType>
            <Content><![CDATA[%s]]></Content>
            <FuncFlag>%d</FuncFlag>
            </xml>";
        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $funcFlag);
        return $resultStr;
    }

    private function transmitNews($object, $arr_item, $funcFlag = 0)
    {
        //首条标题28字,其他标题39字
        if(!is_array($arr_item))
            return;

        $itemTpl = "<item>
            <Title><![CDATA[%s]]></Title>
            <Description><![CDATA[%s]]></Description>
            <PicUrl><![CDATA[%s]]></PicUrl>
            <Url><![CDATA[%s]]></Url>
            </item>";
        $item_str = "";
        foreach ($arr_item as $item)
            $item_str .= sprintf($itemTpl, $item[‘Title‘], $item[‘Description‘], $item[‘PicUrl‘], $item[‘Url‘]);

        $newsTpl = "<xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[news]]></MsgType>
            <Content><![CDATA[]]></Content>
            <ArticleCount>%s</ArticleCount><Articles>
            $item_str</Articles>
            <FuncFlag>%s</FuncFlag>
            </xml>";

        $resultStr = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item), $funcFlag);
        return $resultStr;
    }

}
?>

微信验证及自动回复

标签:

原文地址:http://blog.csdn.net/ly_fireworks/article/details/51352895

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!