zoukankan      html  css  js  c++  java
  • 微信关注与取消关注事件推送

    <?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;
    }
    }
    ?>
  • 相关阅读:
    《大型网站技术架构》读后感
    质量属性
    课堂作业02
    课堂作业01
    《软件构架实践》读后感06
    《软件构架实践》读后感05
    Storm系列三: Storm消息可靠性保障
    Storm系列二: Storm拓扑设计
    Storm系列一: Storm初步
    网络层协议
  • 原文地址:https://www.cnblogs.com/zhouqi666/p/6781243.html
Copyright © 2011-2022 走看看