您好,欢迎访问一九零五行业门户网

微信开发配置文件详细注释版

valid();//变量使用->访问类中的valid()方法,下文valid()方法为验证开发模式接口。这行作用就是验证接口,验证完可注释掉;
$wechatobj->responsemsg();
class wechatcallbackapitest//定义一个类,类为面向对象开发的封装方式;
{
    public function valid()//定义一个公有的名为valid的方法,即验证接口的方法;
    {
        $echostr = $_get[echostr];//从微信用户端获取一个随机字符串赋予变量$echostr;
        //valid signature , option
        if($this->checksignature()){//访问checksignature签名验证方法,若签名一致,输出变量$echostr,完整验证配置接口的操作;
            echo $echostr;
            exit;
        }//签名及接口验证;
    }
    public function responsemsg()//定义一个方法;
    {
        //get post data, may be due to the different environments获取用户端发来的信息,不同的环境可能有差异;
        $poststr = $globals[http_raw_post_data];
          //extract post data解析用户数据;
        if (!empty($poststr)){//判断用户端信息是否为非空;
                /* libxml_disable_entity_loader is to prevent xml external entity injection,
                   the best way is to check the validity of xml by yourself */
                libxml_disable_entity_loader(true);
                  $postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);//将$poststrl变量进行解析并赋予变量$postobj。simplexml_load_string()函数用于解析xml,参数simplexmlelement为新对象的类,libxml_nocdata表示将cdata设置为文本节点,cdata标签中的文本xml不进行解析。
                $fromusername = $postobj->fromusername;//将微信用户端的openid赋予变量$fromusername;
                $tousername = $postobj->tousername;//将公众号id赋予变量$tousername;
                $keyword = trim($postobj->content);//将用户微信发来的文本内容去掉空格后赋予变量$keyword;
                $time = time();//将系统时间赋予变量$time;
                $event = $postobj->event;//这是一个事件获取;
                $texttpl =
%s
0
                            ;    //构建xml格式的文本赋予变量$texttpl。注意xml中的格式为微信内容固定格式。
                            if($event==subscribe)//subscribe是收到订阅信息,event是事件类型,subscribe(订阅)、unsubscribe(取消订阅)
                {
                    $texttpl =
%s
0
                            ;  
                            $msgtype =text;
                            $contentstr = 谢谢关注,你可以回复点什么;
     $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
                echo $resultstr;    
                    }///关注后自动回复文本消息
                            //switch($keyword)
                            //case ;             
                if(!empty( $keyword ))
                {
                      $msgtype = text;//回复文本信息类型为text型,变量类型为$msgtype;
                    $contentstr = 报修,快递查询,饭卡挂失,失物招领,校园街景,新生报到,成绩查询;//$contentstr为回复的信息;
                    $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);//sprintf()为变参函数,$texttpl由于有5个%s,需要5个变量进行赋值。此行代码为关键,sprintf()函数要深入理解。
                    echo $resultstr;//输出回复信息,即发送微信;
                }else{
                    echo input something...;//输入内容,此消息不会发送到微信端,只是测试时使用。
                }
        }else {
            echo ;//微信端没有消息时,回复为空,无意义,测试用;
            exit;//退出;
        }
    }
private function checksignature()//传说中的签名验证程序,此行建立私有方法验证签名,这个私有的checksignature方法,被第18行代码调用,官方文档为加密/校验流程:将token,timestamp,nonce这三个参数进行字典序排序,将这三个参数字符串拼接成一个字符串进行shal加密,开发者获得加密后字符串可与signature对比,标示该请求来源于微信;
    {
        // you must define token by yourself
        if (!defined(token)) {
            throw new exception('token is not defined!');
        }
$signature = $_get[signature];//从用户端获取签名赋予变量$signature;
        $timestamp = $_get[timestamp];//从用户端获取时间戳赋予变量$timestamp;
        $nonce = $_get[nonce];//从用户端获取随机数赋予变量$nonce;
$token = token;
        $tmparr = array($token, $timestamp, $nonce);//建立数组变量$tmparr;
        // use sort_string rule
        sort($tmparr, sort_string);//新建名排序;
        $tmpstr = implode( $tmparr );//字典排序;
        $tmpstr = sha1( $tmpstr );//shal加密;
if( $tmpstr == $signature ){//$tmpstr与$signature变量同值,返回真,否则返回假;
            return true;
        }else{
            return false;
        }
    }
}
?>
以上就介绍了微信开发配置文件详细注释版,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。
其它类似信息

推荐信息