Last active
December 18, 2015 01:19
-
-
Save sang4lv/5703008 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Weixin { | |
| public function __construct($postStr) { | |
| if( !empty($_GET['echostr']) && $this->check_signature() ) { | |
| echo $_GET['echostr']; | |
| return; | |
| } | |
| if( empty($postStr) ) { | |
| exit('no data received'); | |
| } | |
| $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); | |
| $this->ToUserName = $postObj->ToUserName; | |
| $this->FromUserName = $postObj->FromUserName; | |
| $this->CreateTime = $postObj->CreateTime; | |
| $this->MsgType = $postObj->MsgType; | |
| $this->Content = $postObj->Content; | |
| $this->textTemplate = " | |
| <xml> | |
| <ToUserName><![CDATA[%s]]></ToUserName> | |
| <FromUserName><![CDATA[%s]]></FromUserName> | |
| <CreateTime>%s</CreateTime> | |
| <MsgType><![CDATA[%s]]></MsgType> | |
| <Content><![CDATA[%s]]></Content> | |
| <FuncFlag>0</FuncFlag> | |
| </xml> | |
| "; | |
| switch ($this->MsgType) { | |
| default: | |
| $this->process_text( $this->Content ); | |
| break; | |
| } | |
| } | |
| protected function check_signature() { | |
| $signature = $_GET["signature"]; | |
| $timestamp = $_GET["timestamp"]; | |
| $nonce = $_GET["nonce"]; | |
| $token = "****"; | |
| $tmpArr = array( $token, $timestamp, $nonce ); | |
| $tmpStr = sha1( implode( sort( $tmpArr ) ) ); | |
| return ($tmpStr == $signature); | |
| } | |
| protected function process_text( $keyword ) { | |
| $time = time(); | |
| $msgType = "text"; | |
| $content = "blahblah"; | |
| $output = sprintf($this->textTemplate, $this->ToUserName, $this->FromUserName, $time, $msgType, $content); | |
| echo $output; | |
| return; | |
| } | |
| } | |
| $weixin = new Weixin( $GLOBALS["HTTP_RAW_POST_DATA"] ); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment