/* Fang times Studio Copyrights 2014 All Rights Reserved */ Define ("TOKEN", "weixin "); $ WechatObj = new wechatCallbackapiTest (); If (! Isset ($ _ GET ['echostr']) { $ WechatObj-> responseMsg (); } Else { $ WechatObj-> valid (); } Class wechatCallbackapiTest { // Verify the signature Public function valid () { $ EchoStr = $ _ GET ["echostr"]; $ 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 ){ Echo $ echoStr; Exit; } } // Response message 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 ); // Message type separation Switch ($ RX_TYPE) { Case "event ": $ Result = $ this-> receiveEvent ($ postObj ); Break; Case "text ": $ Result = $ this-> deleetext ($ postObj ); Break; } Echo $ result; } Else { Echo ""; Exit; } } // Receives event messages Private function receiveEvent ($ object) { $ Content = ""; Switch ($ object-> Event) { Case "subscribe ": $ Content = "Welcome to fantimes studio \ n reply 2048 to start the game "; Break; } If (is_array ($ content )){ If (isset ($ content [0]) { $ Result = $ this-> transmitNews ($ object, $ content ); } Else if (isset ($ content ['musicurl']) { $ Result = $ this-> transmitMusic ($ object, $ content ); } } Else { $ Result = $ this-> transmitText ($ object, $ content ); } Return $ result; } // Receives text messages Private function deleetext ($ object) { $ Keyword = trim ($ object-> Content ); If (strstr ($ keyword, "2048 ")){ $ Content = array (); $ Content [] = array ("Title" => "2048 games", "Description" => "The game rule is simple. you can choose one of the top, bottom, and left to slide each time, every time you slide, all the digital blocks will move closer to the sliding direction, and the system will also display a number block in the blank space, blocks with the same number are added when they are close together and collided. The number square given by the system is either 2 or 4. players must find a way to generate the "2048" number square in this small 16-frame range. "," PicUrl "=>" http://img.laohu.com/www/201403/27/1395908994962.png "," Url "=>" http://gabrielecirulli.github.io/2048 "); } Else { $ Content = date ("Y-m-d H: I: s", time (). "\ n "; } If (is_array ($ content )){ If (isset ($ content [0] ['picurl']) { $ Result = $ this-> transmitNews ($ object, $ content ); } Else if (isset ($ content ['musicurl']) { $ Result = $ this-> transmitMusic ($ object, $ content ); } } Else { $ Result = $ this-> transmitText ($ object, $ content ); } } // Reply to text message Private function transmitText ($ object, $ content) { $ XmlTpl =" %s %s % S text %s "; $ Result = sprintf ($ xmlTpl, $ object-> FromUserName, $ object-> ToUserName, time (), $ content ); Return $ result; } // Reply to text message Private function transmitNews ($ object, $ newsArray) { If (! Is_array ($ newsArray )){ Return; } $ ItemTpl =" <! [CDATA [% s]> %s %s %s "; $ Item_str = ""; Foreach ($ newsArray as $ item ){ $ Item_str. = sprintf ($ itemTpl, $ item ['title'], $ item ['description'], $ item ['picurl'], $ item ['URL']); } $ XmlTpl =" %s %s % S news % S $ Item_str "; $ Result = sprintf ($ xmlTpl, $ object-> FromUserName, $ object-> ToUserName, time (), count ($ newsArray )); Return $ result; } } ?> |