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;
    }
    }
    ?>
  • 相关阅读:
    intent-filter 之 data 「scheme, host, port, mimeType, path, pathPrefix, pathPattern」
    Android 自定义RecyclerView 实现真正的Gallery效果
    Android手势监听类GestureDetector的使用
    Android 屏幕手势滑动中onFling()函数的技巧分析
    onTouchEvent方法的使用
    Android 手势&触摸事件
    android关闭日志
    android intent和intent action大全
    调用新浪微博显示用户信息
    android 反编译
  • 原文地址:https://www.cnblogs.com/zhouqi666/p/6781243.html
Copyright © 2011-2022 走看看