微信自定义菜单

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
  * wechat php test
  */
 
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
 
$wechatObj->responseMsg();
 
 
$appid="XXXX";//填写appid
$secret="XXXXX";//填写secret
 
$wechatObj->create_menu($appid,$secret);//创建菜单
 
 
class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];
 
        //valid signature , option
        if($this->checkSignature()){
            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)){
                 
                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                $textTpl = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            <FuncFlag>0</FuncFlag>
                            </xml>";            
                if(!empty( $keyword ))
                {
                    $msgType = "text";
                    $contentStr = "123Welcome to the Macau International Airport Micro message platform, Micro message is being tested, the related query to Micro message platform account wechat1@aims.com.mo associated query!~!~";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    echo $resultStr;
                }else{
                    echo "Input something...";
                }
 
        }else {
            echo "";
            exit;
        }
    }
         
    private 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;
        }
    }
     
     
    public function get_access_token($appid,$secret) {
        //请求地址
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
        $ch = curl_init();//模拟地址请求地址
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $a = curl_exec($ch);//获取地址
        curl_close($ch); //关闭
        $strjson=json_decode($a);//json解析
        $access_token = $strjson->access_token;//获取access_token
         
        return $access_token;
    }
     
    //创建菜单
    public  function   create_menu($appid,$secret){
      
     $access_token = $this->get_access_token($appid,$secret);
         
        //构建button
        $post= '{
        "button": [
        {
            "name": "航班信息",
            "sub_button": [
                {
                    "type": "view",
                    "name": "实事航班信息",
                    "url": "http://www.macau-airport.com"
                },
                {
                    "type": "view",
                    "name": "测试",
                    "url": "http://www.macau-airport.com"
                },
                {
                    "type": "view",
                    "name": "航班时刻表",
                    "url": "http://www.macau-airport.com"
                }
            ]
        },
        {
            "name": "机场服务",
            "sub_button": [
                {
                    "type": "view",
                    "name": "旅行预订服务",
                    "url": "http://www.macau-airport.com"
                },
                {
                    "type": "view",
                    "name": "豪华专车服务",
                    "url": "http://www.macau-airport.com"
                },
                {
                    "type": "view",
                    "name": "直通快线服务",
                    "url": "http://www.macau-airport.com"
                }
            ]
        },
        {
            "name": "机场设施",
            "sub_button": [
                {
                    "type": "view",
                    "name": "银行",
                    "url": "http://www.macau-airport.com"
                },
                {
                    "type": "view",
                    "name": "免税店",
                    "url": "http://www.macau-airport.com"
                },
                {
                    "type": "view",
                    "name": "百福小厨",
                    "url": "http://www.macau-airport.com"
                },
                {
                    "type": "view",
                    "name": "行李存放",
                    "url": "http://www.macau-airport.com"
                },
                {
                    "type": "view",
                    "name": "更多",
                    "url": "http://www.macau-airport.com"
                }
            ]
        }
      ]}'; //提交内容
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$access_token}"; //查询地址
         
         
        $ch = curl_init();//新建curl
        curl_setopt($ch, CURLOPT_URL, $url);//url 
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_POST, 1);  //post
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);//post内容 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_exec($ch); //输出  
        curl_close($ch); //关闭
         
         
    }
}
 
?>

  

posted @   麦田守望者~  阅读(208)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示