在使用通用接口前,你需要做以下兩步工作:
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();
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持幫客之家。