目的
創(chuàng)建自定義菜單,實(shí)現(xiàn)菜單事件。
首先獲取Access_Token
接口:
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credentialappid=APPIDsecret=APPSECRET
我用的是測(cè)試號(hào),修改APPID和APPSECRET,然后瀏覽器訪問上面這個(gè)Url即可生成Access_Token
然后配置菜單的事件,caidan.php
?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":"首頁",
"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);
瀏覽器訪問caidan.php
正確時(shí)的返回JSON數(shù)據(jù)包如下:
{"errcode":0,"errmsg":"ok"}
錯(cuò)誤時(shí)的返回JSON數(shù)據(jù)包如下(示例為無效菜單名長度):
{"errcode":40018,"errmsg":"invalid button name size"}
總結(jié)
以上所述是小編給大家介紹的php實(shí)現(xiàn)微信公眾號(hào)創(chuàng)建自定義菜單功能的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
您可能感興趣的文章:- php微信公眾號(hào)開發(fā)模式詳解
- PHP實(shí)現(xiàn)微信公眾號(hào)驗(yàn)證Token的示例代碼
- PHP實(shí)現(xiàn)的微信公眾號(hào)掃碼模擬登錄功能示例
- 微信公眾平臺(tái)開發(fā)教程③ PHP實(shí)現(xiàn)微信公眾號(hào)支付功能圖文詳解
- php微信公眾號(hào)開發(fā)之快遞查詢
- php微信公眾號(hào)開發(fā)之校園圖書館
- php微信公眾號(hào)開發(fā)之歡迎老朋友
- php微信公眾號(hào)開發(fā)之關(guān)鍵詞回復(fù)
- php微信公眾號(hào)開發(fā)之圖片回復(fù)
- 基于PHP的微信公眾號(hào)的開發(fā)流程詳解