微信二次开发自定义接受事件推送的代码

  1. <?php  
  2.   
  3. /** 
  4.   * wechat php test 
  5.   */  
  6.   
  7. //define your token  
  8. define("TOKEN", "<span style="color:#ff0000;">xxxxx</span>");  
  9. $wechatObj = new wechatCallbackapiTest();  
  10. $wechatObj->responseMsg();  
  11.   
  12. class wechatCallbackapiTest {  
  13.     public function valid() {  
  14.         $echoStr = $_GET["echostr"];  
  15.   
  16.         //valid signature , option  
  17.         if ($this->checkSignature()) {  
  18.             echo $echoStr;  
  19.             exit;  
  20.         }  
  21.     }  
  22.   
  23.     public function responseMsg() {  
  24.         //get post data, May be due to the different environments  
  25.         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];  
  26.   
  27.         //extract post data  
  28.         if (!empty ($postStr)) {  
  29.   
  30.             $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);  
  31.             $fromUsername = $postObj->FromUserName;  
  32.             $toUsername = $postObj->ToUserName;  
  33.             $keyword = trim($postObj->Content);  
  34.             $Event = trim($postObj->Event);  
  35.             $time = time();  
  36.             $textTpl = "<xml>  
  37.                         <ToUserName><![CDATA[%s]]></ToUserName>  
  38.                         <FromUserName><![CDATA[%s]]></FromUserName>  
  39.                         <CreateTime>%s</CreateTime>  
  40.                         <MsgType><![CDATA[%s]]></MsgType>  
  41.                         <Content><![CDATA[%s]]></Content>  
  42.                         <FuncFlag>0</FuncFlag>  
  43.                         </xml>";  
  44.             //测试事件推送  
  45.             if($Event=="subscribe"){  
  46.                 $contentStr = "欢迎订阅果晶晶网络科技有限公司公众平台!";  
  47.                 $msgType = "text";  
  48.                 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);  
  49.                 echo $resultStr;  
  50.             }  
  51.             //test  
  52.             if($keyword==1){//文本  
  53.                 $contentStr = $keyword . "测试发送文本!";  
  54.                 $msgType = "text";  
  55.                 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);  
  56.                 echo $resultStr;  
  57.             }else if($keyword==2){//音乐  
  58.                 $textTpl="<xml>  
  59.                          <ToUserName><![CDATA[%s]]></ToUserName>  
  60.                          <FromUserName><![CDATA[%s]]></FromUserName>  
  61.                          <CreateTime>%s</CreateTime>  
  62.                          <MsgType><![CDATA[%s]]></MsgType>  
  63.                          <Music>  
  64.                          <Title><![CDATA[%s]]></Title>  
  65.                          <Description><![CDATA[%s]]></Description>  
  66.                          <MusicUrl><![CDATA[%s]]></MusicUrl>  
  67.                          <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>  
  68.                          </Music>  
  69.                          <FuncFlag>0</FuncFlag>  
  70.                          </xml>";  
  71.                   
  72.                 $msgType = "music";  
  73.                 $msgtitle="致青春";  
  74.                 $Description="电影《致我们终将逝去的青春》 主题曲";  
  75.                 $MusicUrl="http://music.baidu.com/song/40153340#";  
  76.                 $HQMusicUrl="http://music.baidu.com/song/40153340#";  
  77.                 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $msgtitle,$Description,$MusicUrl,$HQMusicUrl);  
  78.                 echo $resultStr;  
  79.             }else if($keyword==3){//图文  
  80.                 $textTpl="<xml>  
  81.                          <ToUserName><![CDATA[%s]]></ToUserName>  
  82.                          <FromUserName><![CDATA[%s]]></FromUserName>  
  83.                          <CreateTime>%s</CreateTime>  
  84.                          <MsgType><![CDATA[%s]]></MsgType>  
  85.                          <ArticleCount>1</ArticleCount>  
  86.                          <Articles>  
  87.                          <item>  
  88.                          <Title><![CDATA[%s]]></Title>   
  89.                          <Description><![CDATA[%s]]></Description>  
  90.                          <PicUrl><![CDATA[%s]]></PicUrl>  
  91.                          <Url><![CDATA[%s]]></Url>  
  92.                          </item>  
  93.                          </Articles>  
  94.                          <FuncFlag>1</FuncFlag>  
  95.                          </xml> ";  
  96.                   
  97.                 $msgType = "news";  
  98.                 $title1="致青春";  
  99.                 $Description="电影《致我们终将逝去的青春》 主题曲";  
  100.                 $PicUrl="http://img10.360buyimg.com/da/g13/M05/01/02/rBEhU1G6iA8IAAAAAADmPyFSPhUAAANiwAxLNcAAOZX575.jpg";  
  101.                 $Url="http://sale.jd.com/act/qYHkylDS0LVOg.html?sid=2&cid=601&aid=3678&bid=661&unit=36617&advid=60724&guv=";  
  102.                 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $title1,$Description,$PicUrl,$Url);  
  103.                 echo $resultStr;  
  104.             }  
  105.             //  
  106.             if (!empty ($keyword)) {  
  107.   
  108.                 $contentStr = $keyword . "Welcome to wechat world!";  
  109.   
  110.                 $msgType = "text";  
  111.   
  112.                 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);  
  113.                 echo $resultStr;  
  114.             } else {  
  115.                 echo "Input something...";  
  116.             }  
  117.   
  118.         } else {  
  119.             echo "";  
  120.             exit;  
  121.         }  
  122.     }  
  123.   
  124.     private function checkSignature() {  
  125.         $signature = $_GET["signature"];  
  126.         $timestamp = $_GET["timestamp"];  
  127.         $nonce = $_GET["nonce"];  
  128.   
  129.         $token = TOKEN;  
  130.         $tmpArr = array (  
  131.             $token,  
  132.             $timestamp,  
  133.             $nonce  
  134.         );  
  135.         sort($tmpArr);  
  136.         $tmpStr = implode($tmpArr);  
  137.         $tmpStr = sha1($tmpStr);  
  138.   
  139.         if ($tmpStr == $signature) {  
  140.             return true;  
  141.         } else {  
  142.             return false;  
  143.         }  
  144.     }  
  145. }  
  146. ?>  

微信二次开发自定义接受事件推送的代码

上一篇:【placehlder修改css样式】


下一篇:微信平台开发应用的签名