生活随笔
收集整理的這篇文章主要介紹了
微信利用PHP创建自定义菜单的方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在使用通用接口前,你需要做以下兩步工作:
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}
創建自定義菜單:
<?
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(); ?
?
轉載于:https://www.cnblogs.com/GmrBrian/p/3612985.html
總結
以上是生活随笔為你收集整理的微信利用PHP创建自定义菜单的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。