基于ThinkPHP5框架做的微信公众号开发(001)

来源:互联网 发布:网络用户管理系统注册 编辑:程序博客网 时间:2024/06/07 19:51
<?phpnamespace app\index\controller;/**  * wechat php test  *///define your tokendefine("TOKEN", "sudons");class Wechat{    public function index(){        if (isset($_GET['echostr'])) {            $this->valid();        }else{            $this->responseMsg();        }    }public function valid()    {        $echoStr = $_GET["echostr"];        //valid signature , option        if($this->checkSignature() && $echoStr){        echo $echoStr;        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 );        //检验signature        if( $tmpStr == $signature){            return true;        }else{            return false;        }    }    public function responseMsg()    {//get post data, May be due to the different environments$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];      //extract post dataif (!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);            $RX_TYPE = trim($postObj->MsgType);            switch ($RX_TYPE)            {                case "text":                    $resultStr = $this->receiveText($postObj);                    break;                case "image":                    $resultStr = $this->receiveImage($postObj);                    break;                case "location":                    $resultStr = $this->receiveLocation($postObj);                    break;                case "voice":                    $resultStr = $this->receiveVoice($postObj);                    break;                case "video":                    $resultStr = $this->receiveVideo($postObj);                    break;                case "link":                    $resultStr = $this->receiveLink($postObj);                    break;                case "event":                    $resultStr = $this->receiveEvent($postObj);                    break;                default:                    $resultStr = "unknow msg type: ".$RX_TYPE;                    break;            }            echo $resultStr;        }else {        echo "";        exit;        }    }    //统一用文本回复    private function transmitText($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;    }    //1.事件消息    private function receiveEvent($object)    {        $contentStr = "";        switch ($object->Event)        {            case "subscribe":                $contentStr = "欢迎关注塞雅创想";                break;            case "unsubscribe":                $contentStr = "";                break;            case "CLICK":                switch ($object->EventKey)                {                    default:                        $contentStr = "你点击了菜单: ".$object->EventKey;                        break;                }                break;            default:                $contentStr = "receive a new event: ".$object->Event;                break;        }        $resultStr = $this->transmitText($object, $contentStr);        return $resultStr;    }    //2.文本消息    private function receiveText($object)    {        $funcFlag = 0;        $contentStr = "你发送的是文本,内容为:".$object->Content;        $resultStr = $this->transmitText($object, $contentStr, $funcFlag);        return $resultStr;    }    //3.图片消息    private function receiveImage($object)    {        $funcFlag = 0;        $contentStr = "你发送的是图片,地址为:".$object->PicUrl;        $resultStr = $this->transmitText($object, $contentStr, $funcFlag);        return $resultStr;    }    //4.语音消息    private function receiveVoice($object)    {        $funcFlag = 0;        $contentStr = "你发送的是语音,媒体ID为:".$object->MediaId;        $resultStr = $this->transmitText($object, $contentStr, $funcFlag);        return $resultStr;    }    //5.视频消息    private function receiveVideo($object)    {        $funcFlag = 0;        $contentStr = "你发送的是视频,媒体ID为:".$object->MediaId;        $resultStr = $this->transmitText($object, $contentStr, $funcFlag);        return $resultStr;    }    //6.位置消息    private function receiveLocation($object)    {        $funcFlag = 0;        $contentStr = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label;        $resultStr = $this->transmitText($object, $contentStr, $funcFlag);        return $resultStr;    }    //7.链接消息    private function receiveLink($object)    {        $funcFlag = 0;        $contentStr = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url;        $resultStr = $this->transmitText($object, $contentStr, $funcFlag);        return $resultStr;    }}?>
阅读全文
0 0
原创粉丝点击