• 申请测试号进行练习


    1.建立测试号

    1.1 首先需要写服务器连接测试程序,建立连接通路
    
    //demo.php
    define("TOKEN", "weixin");
    
    //接收随机字符串
    $echoStr = $_GET["echostr"];
    
    //进行用户数字签名验证
    if(checkSignature()){
        //如果成功,则返回接收到的随机字符串
        echo $echoStr;
        //退出
        exit;
    }
    
    function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
    	
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    1.2.申请测试号

    测试号申请连接
    正式号和测试号所有开发路径是一样的,但是接口有诸多限制。

    url: 填写demo.php的路径地址【此路径地址外部要是可访问的】
    token:就是第一步中代码中的token
    
    • 1
    • 2

    2.1 根据用户输入回复各种类型消息

    建立通路之后,用户每输入一次消息,微信都会发一个post请求到【1.2】配置的url当中,处理post请求按照模板【各种消息模板】返回即可

            //接收用户端发送过来的XML数据
            $postStr = @file_get_contents('php://input');
    
            //extract post data
            //判断XML数据是否为空
            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);
                //通过simplexml进行xml解析
                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                //手机端
                $fromUsername = $postObj->FromUserName;
                //微信的公众平台
                $toUsername = $postObj->ToUserName;
                //接收用户发送的关键词
                $keyword = trim($postObj->Content);
                //接收用户消息类型
                $msgType = $postObj->MsgType;
                //定义$longitude与$latitude接收用户发送的经纬度信息
                $latitude = $postObj->Location_X;
                $longitude = $postObj->Location_Y;
    
                //接收Event与EventKey参数
                $event = $postObj->Event;
                $eventKey = $postObj->EventKey;
    
                //时间戳
                $time = time();
                //文本发送模板
                $textTpl = "
                                
                                
                                %s
                                
                                
                                0
                                ";
                //音乐发送模板
                $musicTpl = "
                                
                                
                                %s
                                
                                
                                <![CDATA[%s]]>
                                
                                
                                
                                
                                ";
                //图文发送模板
                $newsTpl = "
                                
                                
                                %s
                                
                                %s
                                %s
                                ";
    
                if($msgType=='text') {
                    //判断用户发送关键词是否为空
                    if(!empty( $keyword ))
                    {
                        if($keyword=='文本') {
                            //回复类型,如果为“text”,代表文本类型
                            $msgType = "text";
                            //回复内容
                            $contentStr = "您发送的是文本消息";
                            //格式化字符串
                            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                            //把XML数据返回给手机端
                            echo $resultStr;
                        } elseif($keyword=='?' || $keyword=='?') {
                            //定义回复类型
                            $msgType=='text';
                            //回复内容
                            $contentStr = "【1】特种服务号码\n【2】通讯服务号码\n【3】银行服务号码\n您可以通过输入【】方括号的编号获取内容哦!";
                            //格式化字符串
                            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                            //返回数据到微信客户端
                            echo $resultStr;
                        } elseif ($keyword=='1') {
                            //定义回复类型
                            $msgType=='text';
                            //回复内容
                            $contentStr = "常用特种服务号码:\n匪警:110\n火警:119";
                            //格式化字符串
                            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                            //返回数据到微信客户端
                            echo $resultStr;
                        } elseif ($keyword=='2') {
                            //定义回复类型
                            $msgType=='text';
                            //回复内容
                            $contentStr = "常用通讯服务号码:\n中移动:10086\n中电信:10000";
                            //格式化字符串
                            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                            //返回数据到微信客户端
                            echo $resultStr;
                        } elseif ($keyword=='3') {
                            //定义回复类型
                            $msgType=='text';
                            //回复内容
                            $contentStr = "常用银行服务号码:\n工商银行:95588\n建设银行:95533";
                            //格式化字符串
                            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                            //返回数据到微信客户端
                            echo $resultStr;
                        } elseif ($keyword=='音乐') {
                            //定义回复类型
                            $msgType='music';
                            //定义音乐标题
                            $title = '冰雪奇缘';
                            //定义音乐描述
                            $desc = '《冰雪奇缘》原声大碟...';
                            //定义音乐链接
                            $url = 'http://czbk888.duapp.com/music.mp3';
                            //定义高清音乐链接
                            $hqurl = 'http://czbk888.duapp.com/music.mp3';
                            //格式化字符串
                            $resultStr = sprintf($musicTpl, $fromUsername, $toUsername, $time, $msgType, $title, $desc, $url, $hqurl);
                            //返回XML数据到微信客户端
                            echo $resultStr;
                        } elseif ($keyword=='图文') {
                            //定义回复类型
                            $msgType='news';
                            //定义返回图文数量
                            $count = 4;
                            //组装Articles节点信息
                            $str = '';
                            for($i=1;$i<=$count;$i++) {
                                $str .= "
                                            <![CDATA[微信开发教程<span class="token interpolation"><span class="token punctuation">{<!-- --></span><span class="token variable">$i</span><span class="token punctuation">}</span></span>]]> 
                                            
                                            {$i}.jpg]]>
                                            
                                            ";
                            }
                            $str .= '';
                            //格式化字符串
                            $resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, $msgType, $count, $str);
                            //输出XML数据并返回到微信客户端
                            echo $resultStr;
                        } else {
                            //定义回复类型
                            $msgType='text';
                            //定义url链接操作
                            $url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg={$keyword}";
                            //模拟发送http中的get请求
                            $str = file_get_contents($url);
                            //格式化json字符串为对象或数组
                            $json = json_decode($str);
                            //定义回复内容
                            $contentStr = $json->content;
                            //格式化字符串
                            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                            //返回数据到微信客户端
                            echo $resultStr;
                        }
                    }else{
                        echo "Input something...";
                    }
                }
           }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
  • 相关阅读:
    中集集团全球港航人工智能高科技领军企业中集飞瞳,工业级高性能港航人工智能产品全球规模应用智能化港口船公司铁路堆场海关大幅提效降本
    js的宏任务与微任务
    Eclipse Python IDE安装
    使用FRP进行内网穿透的最佳实践
    为什么要租用高防服务器?
    面试实遇总结
    Spring快速上手
    冲刺第十五届蓝桥杯P0003倍数问题
    Leetcode160. 相交链表
    目标框选之单阶段与两阶段目标检测区别
  • 原文地址:https://blog.csdn.net/u011028571/article/details/127654759