本文主要和大家介紹PHP實現創建微信自訂選單的方法,結合實例形式分析了php創建微信自訂選單的原理、步驟與具體實現技巧,需要的朋友可以參考下,希望能幫助到大家。
在使用通用介面前,你需要做以下兩步驟工作:
#1.擁有一個微信公眾帳號,並且取得到appid和appsecret(在公眾平台申請內測資格,審核通過後可獲得)
#2.透過取得憑證介面取得到access_token
#注意:
access_token是第三方存取api資源的票據;
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid =APPID&secret=APPSECRET
{"access_token":"这里就是你的access_token","expires_in":7200}
<?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();
以上是PHP建立微信自訂選單的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!