微信 关注/取消关注事件

来源:互联网 发布:域名dns解析服务器 编辑:程序博客网 时间:2024/04/29 21:38
<?php/**  * wechat php test  *///define your tokendefine("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();//$wechatObj->valid();if(!isset($_GET["echostr"])){     $wechatObj->responseMsg();}else{ $wechatObj->valid();}class wechatCallbackapiTest{public function valid()    {        $echoStr = $_GET["echostr"];        if($this->checkSignature()){        echo $echoStr;//$this->responseMsg();        exit;        }    }    public function responseMsg()    {$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];     if (!empty($postStr)){        $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);    $RX_TYPE = trim($postObj->MsgType);                switch($RX_TYPE)                {                    case "text":                        $resultStr = $this->handleText($postObj);                        break;                    case "event":                        $resultStr = $this->handleEvent($postObj);                        break;                    default:                        $resultStr = "Unknow msg type: ".$RX_TYPE;                        break;                }                echo $resultStr;                //$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 = "你好啊,屌丝1";                //$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);                //echo $resultStr;                //}else{                //echo "你好啊,屌丝1";                //}        }else {        echo "";        exit;        }    }    public function handleEvent($object)    {        $contentStr = "";        switch ($object->Event)        {            case "subscribe":                $contentStr = "感谢您关注【微卖】"."\n"."我们为您提供全国各地的生活指南,做最好的外卖微信平台。"."\n"."目前平台功能如下:"."\n"."【1】 查天气,如输入:本地天气"."\n"."【2】 查公交,如输入:本地公交178"."\n"."【3】 翻译,如输入:翻译I love you"."\n"."【4】 本地信息查询,如输入:"."\n"."更多内容,敬请期待...";                break;            default :                $contentStr = "Unknow Event: ".$object->Event;                break;        }        $resultStr = $this->responseText($object, $contentStr);        return $resultStr;    }        public function responseText($object, $content, $flag=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, $flag);        return $resultStr;    }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 rulesort($tmpArr, SORT_STRING);$tmpStr = implode( $tmpArr );$tmpStr = sha1( $tmpStr );return true;if( $tmpStr == $signature ){return true;}else{return false;}}}?>

0 0