- <?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;
- }
- }
- }
- ?>
相关文章
- 11-04微信公众号的自定义菜单事件推送
- 11-04.net MVC 微信公众号 点击菜单拉取消息时的事件推送
- 11-04微信公众帐号开发-自定义菜单的创建及菜单事件响应
- 11-04ThinkPHP tp Codeigniter ci 微信开发 控制器代码 示例 实例 模拟http请求 获取acces_token 创建自定义菜单 点击事件 转多客服
- 11-04微信小程序页面调用自定义组件内的事件
- 11-04微信二次开发自定义接受事件推送的代码
- 11-04夺命雷公狗---微信开发17----自定义菜单的事件推送,响应菜单的CLICK
- 11-04微信小程序自定义组件——接受外部传入的样式类
- 11-04微信公众平台推出针对开发者的微信认证事件推送
- 11-04微信推送模板消息的PHP代码整理