Skip to content

Instantly share code, notes, and snippets.

@sang4lv
Last active December 18, 2015 01:19
Show Gist options
  • Save sang4lv/5703008 to your computer and use it in GitHub Desktop.
Save sang4lv/5703008 to your computer and use it in GitHub Desktop.
<?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