微信二次开发自定义接受事件推送的代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信二次开发自定义接受事件推送的代码相关的知识,希望对你有一定的参考价值。
- <?php
- /**
- * wechat php test
- */
- //define your token
- define("TOKEN", "<span style="color:#ff0000;">xxxxx</span>");
- $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)) {
- $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
- $fromUsername = $postObj->FromUserName;
- $toUsername = $postObj->ToUserName;
- $keyword = trim($postObj->Content);
- $Event = trim($postObj->Event);
- $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($Event=="subscribe"){
- $contentStr = "欢迎订阅果晶晶网络科技有限公司公众平台!";
- $msgType = "text";
- $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
- echo $resultStr;
- }
- //test
- if($keyword==1){//文本
- $contentStr = $keyword . "测试发送文本!";
- $msgType = "text";
- $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
- echo $resultStr;
- }else if($keyword==2){//音乐
- $textTpl="<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[%s]]></MsgType>
- <Music>
- <Title><![CDATA[%s]]></Title>
- <Description><![CDATA[%s]]></Description>
- <MusicUrl><![CDATA[%s]]></MusicUrl>
- <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
- </Music>
- <FuncFlag>0</FuncFlag>
- </xml>";
- $msgType = "music";
- $msgtitle="致青春";
- $Description="电影《致我们终将逝去的青春》 主题曲";
- $MusicUrl="http://music.baidu.com/song/40153340#";
- $HQMusicUrl="http://music.baidu.com/song/40153340#";
- $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $msgtitle,$Description,$MusicUrl,$HQMusicUrl);
- echo $resultStr;
- }else if($keyword==3){//图文
- $textTpl="<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[%s]]></MsgType>
- <ArticleCount>1</ArticleCount>
- <Articles>
- <item>
- <Title><![CDATA[%s]]></Title>
- <Description><![CDATA[%s]]></Description>
- <PicUrl><![CDATA[%s]]></PicUrl>
- <Url><![CDATA[%s]]></Url>
- </item>
- </Articles>
- <FuncFlag>1</FuncFlag>
- </xml> ";
- $msgType = "news";
- $title1="致青春";
- $Description="电影《致我们终将逝去的青春》 主题曲";
- $PicUrl="http://img10.360buyimg.com/da/g13/M05/01/02/rBEhU1G6iA8IAAAAAADmPyFSPhUAAANiwAxLNcAAOZX575.jpg";
- $Url="http://sale.jd.com/act/qYHkylDS0LVOg.html?sid=2&cid=601&aid=3678&bid=661&unit=36617&advid=60724&guv=";
- $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $title1,$Description,$PicUrl,$Url);
- echo $resultStr;
- }
- //
- if (!empty ($keyword)) {
- $contentStr = $keyword . "Welcome to wechat world!";
- $msgType = "text";
- $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
- echo $resultStr;
- } else {
- echo "Input something...";
- }
- } else {
- echo "";
- exit;
- }
- }
- private function checkSignature() {
- $signature = $_GET["signature"];
- $timestamp = $_GET["timestamp"];
- $nonce = $_GET["nonce"];
- $token = TOKEN;
- $tmpArr = array (
- $token,
- $timestamp,
- $nonce
- );
- sort($tmpArr);
- $tmpStr = implode($tmpArr);
- $tmpStr = sha1($tmpStr);
- if ($tmpStr == $signature) {
- return true;
- } else {
- return false;
- }
- }
- }
- ?>
以上是关于微信二次开发自定义接受事件推送的代码的主要内容,如果未能解决你的问题,请参考以下文章