zoukankan      html  css  js  c++  java
  • 微信公众号平台开发-事件推送

    事件推送

    1).推送类型介绍

      1 关注/取消关注事件

      2 扫描带参数二维码事件

      3 上报地理位置事件

      4 自定义菜单事件

      5 点击菜单拉取消息时的事件推送

      6 点击菜单跳转链接时的事件推送

     

    推送XML数据包示例:

    <xml><ToUserName>< ![CDATA[toUser] ]></ToUserName><FromUserName>< ![CDATA[FromUser] ]></FromUserName><CreateTime>123456789</CreateTime><MsgType>< ![CDATA[event] ]></MsgType><Event>< ![CDATA[subscribe] ]></Event></xml>
     

    参数说明:

    参数描述
    ToUserName 开发者微信号
    FromUserName 发送方帐号(一个OpenID)
    CreateTime 消息创建时间 (整型)
    MsgType 消息类型,event
    Event 事件类型,subscribe(订阅)、unsubscribe(取消订阅)

     

     上面消息post推送的数据格式--网址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140454

    当用户关注微信公众号时,微信给我们的url网址推送一个post请求,请求数据是xml格式的

    后台设置的url:http://www.cc8w.com/weixin/openweixin.php

    我们需要这样获取:

    $postArr = $_GLOBALS['HTTP_RAW_POST_DATA'];
    file_get_contents("php://input");//这样也可以获取

    这个xml格式.

    //获取到xml数据后,处理消息类型,并设置回复消息内容(回复就是直接打印xml数据)

        public function reposeMsg()
        {//接收事件推送过来的数据,并回复
            //文案地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140453
            $postArr = $GLOBALS['HTTP_RAW_POST_DATA'];//接受推送事件
    
            //数据格式
            $arr = simplexml_load_string($postArr);
            if(strtolower($arr->MsgType)=="event")
            {
                $toUser = $arr->ToUserName;
                $foUser = $arr->FromUserName;
                $msgType = 'text';
                $createTime = time();
                $content = '尊敬的'.$foUser."谢谢你的关注
    ";
    
                if(strtolower($arr->Event)=="subscribe")
                {//订阅事件的处理
                    $temp = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content></xml>";
                    $temp = sprintf($temp,$foUser,$toUser,$createTime,$msgType,$content);
                    return $temp;
                }
            }
        }

    实验的地址:https://gitee.com/fps2tao/openweixin

  • 相关阅读:
    tkinter gui界面使用调戏妹子
    @property 用法
    @classmehod 用法解析
    python psutil 使用和windows 10 设置
    python 类多重继承问题
    多线程同步引入锁
    Linux—禁止用户SSH登录方法总结
    Linux FTP的主动模式与被动模式
    Java Socket详解
    MySQL学习(一)——创建新用户、数据库、授权
  • 原文地址:https://www.cnblogs.com/fps2tao/p/8666065.html
Copyright © 2011-2022 走看看