在使用通用接口前,你需要做以下兩步工作:
1.擁有一個(gè)微信公眾賬號(hào),并獲取到appid和appsecret(在公眾平臺(tái)申請(qǐng)內(nèi)測(cè)資格,審核通過(guò)后可獲得)
2.通過(guò)獲取憑證接口獲取到access_token
注意:
access_token是第三方訪問(wèn)api資源的票據(jù);
access_token對(duì)應(yīng)于公眾號(hào)是全局唯一的票據(jù),重復(fù)獲取將導(dǎo)致上次獲取的access_token失效。
訪問(wèn)下面這個(gè)地址(注意替換你的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}
創(chuàng)建自定義菜單:
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" ); //創(chuàng)建菜單 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" : "首頁(yè)" , "key" : "home" }, { "type" : "click" , "name" : "簡(jiǎn)介" , "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(); |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。