zhouqi666
<?php
//
// 关注/取消关注事件消息
// 微信公众账号关注与取消关注事件消息
//

define("TOKEN", "zhouqi");

$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET[\'echostr\'])) {
$wechatObj->responseMsg();
}else{
$wechatObj->valid();
}

class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}

private function checkSignature()
{
$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){
return true;
}else{
return false;
}
}

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);
break;
}
echo $result;
}else {
echo "";
exit;
}
}

private function receiveEvent($object)
{
$content = "";
switch ($object->Event)
{
case "subscribe": //关注事件
$content = "欢迎关注周起,么么哒(づ ̄ 3 ̄)づ";
break;
case "unsubscribe": //取消关注事件
$content = "";
break;
}
$result = $this->transmitText($object, $content);
return $result;
}

private function transmitText($object, $content)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
return $result;
}
}
?>

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-10-20
  • 2022-02-21
  • 2021-12-16
  • 2022-01-07
  • 2021-09-11
  • 2022-12-23
猜你喜欢
  • 2021-11-28
  • 2021-11-01
  • 2022-12-23
  • 2021-11-17
  • 2021-12-16
相关资源
相似解决方案