/* Square Double Studio CopyRight All Rights Reserved */ Define ("TOKEN", "Weixin"); $WECHATOBJ = new Wechatcallbackapitest (); if (!isset ($_get[' echostr ')) { $WECHATOBJ->responsemsg (); }else{ $WECHATOBJ->valid (); } Class Wechatcallbackapitest { Verifying signatures 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->receivetext ($POSTOBJ); Break } echo $result; }else { echo ""; Exit } } Receiving event Messages Private Function Receiveevent ($object) { $content = ""; Switch ($object->event) { Case "Subscribe": $content = "Welcome to double studio \ n Reply 2048 start 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; } Receive text messages Private Function Receivetext ($object) { $keyword = Trim ($object->content); if (Strstr ($keyword, "2048")) { $content = Array (); $content [] = Array ("Title" = "2048 Game", "Description" and "=" game rule is very simple, each can choose to swipe up and down in one direction, each slide once, all the digital blocks will move in the direction of sliding, The system will also appear in the blank place a number of random squares, the same number of squares in close, collision will be added. The system gives the number box is not 2 is 4, the player should find a way in this small 16-block range of "2048" this number block. "," 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 Technical support double studio "; } 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 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 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; } } ?> |