在使用通用接口前,你需要做以下兩步工作:
1.擁有一個微信公眾賬號,并獲取到appid和appsecret(在公眾平臺申請內測資格,審核通過后可獲得)
2.通過獲取憑證接口獲取到access_token
注意:
access_token是第三方訪問api資源的票據;
access_token對應于公眾號是全局唯一的票據,重復獲取將導致上次獲取的access_token失效。
訪問下面這個地址(注意替換你的appid和secret):
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
然后在瀏覽器能看到一下返回信息:
{"access_token":"這里就是你的access_token","expires_in":7200}
創建自定義菜單:
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
|
<?php header( "Content-type: text/html; charset=utf-8" ); define( "ACCESS_TOKEN" , "這里填入你上面獲取到的access_token" ); //創建菜單 function createMenu( $data ){ $ch = curl_init(); curl_setopt( $ch , CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" .ACCESS_TOKEN); curl_setopt( $ch , CURLOPT_CUSTOMREQUEST, "POST" ); curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt( $ch , CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)' ); curl_setopt( $ch , CURLOPT_FOLLOWLOCATION, 1); curl_setopt( $ch , CURLOPT_AUTOREFERER, 1); curl_setopt( $ch , CURLOPT_POSTFIELDS, $data ); curl_setopt( $ch , CURLOPT_RETURNTRANSFER, true); $tmpInfo = curl_exec( $ch ); if (curl_errno( $ch )) { return curl_error( $ch ); } curl_close( $ch ); return $tmpInfo ; } //獲取菜單 function getMenu(){ return file_get_contents ( "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" .ACCESS_TOKEN); } //刪除菜單 function deleteMenu(){ return file_get_contents ( "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" .ACCESS_TOKEN); } $data = '{ "button" :[ { "type" : "click" , "name" : "首頁" , "key" : "home" }, { "type" : "click" , "name" : "簡介" , "key" : "introduct" }, { "name" : "菜單" , "sub_button" :[ { "type" : "click" , "name" : "hello word" , "key" : "V1001_HELLO_WORLD" }, { "type" : "click" , "name" : "贊一下我們" , "key" : "V1001_GOOD" }] }] }'; echo createMenu( $data ); //echo getMenu(); //echo deleteMenu(); |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。