微信开发中消息接收、回复及事件处理通用函数与php语法的详解(index.php文件)

<?php
/*
    方倍工作室 http://www.fangbei.org/
    CopyRight 2015 All Rights Reserved
*/
header('Content-type:text');


define("TOKEN", "weixin"); //定义TOKEN=weixin
$wechatObj = new wechatCallbackapiTest(); //新建一个wechatCallbackapiTest类型对象wechatObj
if (!isset($_GET['echostr'])) { //echostr不存在的话去执行wechatObj类中的responseMsg函数
    $wechatObj->responseMsg();
}else{
    $wechatObj->valid(); //echostr存在的话去执行wechatObj类中的valid函数
}


class wechatCallbackapiTest
{
    //验证签名
    public function valid()
    {
        $echoStr = $_GET["echostr"]; //从前台url中取出参数echostr的值再赋值给变量echoStr()
        $signature = $_GET["signature"]; //从前台url中取出参数signature的值再赋值给变量signature(签名)
        $timestamp = $_GET["timestamp"]; //从前台url中取出参数timestamp的值再赋值给变量timestamp(时间戳)
        $nonce = $_GET["nonce"]; //从前台url中取出参数nonce的值再赋值给变量nonce(随机数、临时数)
        $token = TOKEN; //将TOKEN赋值给变量token(就是固定值“weixin”)
        $tmpArr = array($token, $timestamp, $nonce); //将被赋值的三个变量$token, $timestamp, $nonce组成数组tmpArr
        sort($tmpArr, SORT_STRING); //给数组tmpArr中三个元素进行排序
        $tmpStr = implode($tmpArr); //将数组tmpArr通过 implode函数解析成字符串并赋值给tmpStr
        $tmpStr = sha1($tmpStr); //将字符串tmpStr通过sha1函数进行加密后重新赋值给tmpStr
        if($tmpStr == $signature){ //判断签名的字符串signature与新生成的tmpStr字符串相等的话输出echoStr
            echo $echoStr;
            exit;
        }
    }


    //响应消息
    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //取post表单类数据中的所有值并赋值给postStr变量
        if (!empty($postStr)){
            $this->logger("R \r\n".$postStr); //通过日志函数logger记录日志,. 用在两个字符串之间是连接字符串的意思相当于+号
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); //将字符串变量postStr按SimpleXMLElement格式只取CDATA标签中的值并转化成对象型变量$postObj
            $RX_TYPE = trim($postObj->MsgType); //获取对象型变量postObj的MsgType属性值并赋值给RX_TYPE变量
            
            //消息类型分离
            switch ($RX_TYPE)
            { //根据前边被赋过值的RX_TYPE变量来判断去执行的不同的函数
                case "event":
                    $result = $this->receiveEvent($postObj);
                    break;
                case "text":
                    $result = $this->receiveText($postObj);
                    break;
                case "image":
                    $result = $this->receiveImage($postObj);
                    break;
                case "location":
                    $result = $this->receiveLocation($postObj);
                    break;
                case "voice":
                    $result = $this->receiveVoice($postObj);
                    break;
                case "video":
case "shortvideo":
                    $result = $this->receiveVideo($postObj);
                    break;
                case "link":
                    $result = $this->receiveLink($postObj);
                    break;
                default:
                    $result = "unknown msg type: ".$RX_TYPE;
                    break;
            }
            $this->logger("T \r\n".$result);
            echo $result;
        }else {
            echo "";
            exit;
        }
    }


    //接收事件消息
    private function receiveEvent($object)
    {
    &nbs
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值