微信公众号 获取推送消息

 1. 第一次验签

 2. 关注或取消关注后就会推送消息

 3. 一定要开启服务器配置,开启后需要过会儿生生效

<?php
/**
 * Created by IntelliJ IDEA.
 * User: mzc
 * Date: 2019-11-09
 * Time: 20:28
 */
//ini_set("display_errors", "On");
//error_reporting(E_ALL | E_STRICT);
//exit("22222");
class wx {

    private  $_token = "dxxw";
    /*
    用于第一次验证我们网站url合法性
     */
    public function firstValid(){
        $this->log(1111);
        //检验签名的合法性
        if($this->_checkSignature()){
            //签名合法,告知微信公众平台服务器
            echo $_GET['echostr'];
        }
    }
    /**
     * 验证签名
     * @return bool
     */
    private function _checkSignature()
    {
        $this->log(2222);

        //获得由微信公众平台请求的验证数据
        $signature = $_GET['signature'];
        $timestamp = $_GET['timestamp'];
        $nonce = $_GET['nonce'];
        //将时间戳,随机字符串,token按照字母顺序排序,病并连接
        $tmp_arr = array($this->_token, $timestamp, $nonce);
        sort($tmp_arr, SORT_STRING);//字典顺序
        $tmp_str = implode($tmp_arr);//连接
        $tmp_str = sha1($tmp_str);//sha1加密
        if ($signature == $tmp_str) {
            $this->log("验证成功");
            return true;
        } else {
            $this->log("验证失败");
            return false;
        }
    }

    public function responseMsg(){
        /*
        获得请求时POST:XML字符串
        不能用$_POST获取,因为没有key
         */
        mkdir(111,0777);
        $xml_str = !empty($GLOBALS['HTTP_RAW_POST_DATA'])?$GLOBALS['HTTP_RAW_POST_DATA']:$_POST;
        if(empty($xml_str)){
            $xml_str =  file_get_contents('php://input');
        }
        $this->log($xml_str);

//        if(!empty($xml_str)){
//            // 解析该xml字符串,利用simpleXML
//            libxml_disable_entity_loader(true);
//            //禁止xml实体解析,防止xml注入
//            $request_xml = simplexml_load_string($xml_str, 'SimpleXMLElement', LIBXML_NOCDATA);
//            //判断该消息的类型,通过元素MsgType
//            switch ($request_xml->MsgType){
//                case 'event':
//                    //判断具体的时间类型(关注、取消、点击)
//                    $event = $request_xml->Event;
//                    if ($event=='subscribe') { // 关注事件
//                        $this->_doSubscribe($request_xml);
//                    }elseif ($event=='CLICK') {//菜单点击事件
//                        $this->_doClick($request_xml);
//                    }elseif ($event=='VIEW') {//连接跳转事件
//                        $this->_doView($request_xml);
//                    }else{
//
//                    }
//                    break;
//                case 'text'://文本消息
//                    $this->_doText($request_xml);
//                    break;
//                case 'image'://图片消息
//                    $this->_doImage($request_xml);
//                    break;
//                case 'voice'://语音消息
//                    $this->_doVoice($request_xml);
//                    break;
//                case 'video'://视频消息
//                    $this->_doVideo($request_xml);
//                    break;
//                case 'shortvideo'://短视频消息
//                    $this->_doShortvideo($request_xml);
//                    break;
//                case 'location'://位置消息
//                    $this->_doLocation($request_xml);
//                    break;
//                case 'link'://链接消息
//                    $this->_doLink($request_xml);
//                    break;
//            }
//        }
    }

    public function log($content){
        $logFile = "1.log";
        if(is_array($content)){
            $content = var_export($content,true);
        }
        $newLog = date('Y-m-d H:i:s').":".$content;
        file_put_contents($logFile, $newLog.PHP_EOL, FILE_APPEND);
    }

}

$wx = new wx();
if(!empty($_GET['echostr'])){
    $wx->firstValid(); //验签
}else{
    $wx->responseMsg();//接收关注或取消关注后的推送消息
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值