define("TOKEN", "w3note");//定义识别码
$wechatObj = new wechatCallbackapiTest();//实例化wechatCallbackapiTest类
if(!isset($_GET["echostr"])){
$wechatObj->responseMsg();
}else{
$wechatObj->valid();
}
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
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);
switch($RX_TYPE){
case "event":
$result = $this->receiveEvent($postObj);
breadk;
}
echo $result;
}else{
echo "";
exit;
}
}
private function receiveEvent($object){
$content = "";
switch ($postObj->Event){
case "subscribe":
$content = "欢迎关注网志博客";//这里是向关注者发送的提示信息
break;
case "unsubscribe":
$content = "";
break;
}
$result = $this->transmitText($object,$content);
return $result;
}
private function transmitText($object,$content){
$textTpl = "
%s
0
";
$result = sprintf($textTpl, $object->FromUserName, $object->$ToUserName, time(), $content);
return $result;
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
|