首頁 微信小程式 微信開發 分享微信開發自訂選單的處理範例程式碼

分享微信開發自訂選單的處理範例程式碼

Mar 14, 2017 pm 02:44 PM

在微信5.0以前,自訂選單是作為一種內測資格使用的,只有少數公眾帳號擁有選單,因此出現很多企業為了弄到選單不惜重金求購。現如今,一大批帳號從訂閱號碼轉為服務號,很多都是奔著自訂選單去的。今天我們就來簡單研究下微信自訂選單的處理。

自訂選單的建立

<?php
 
define("APPID", "您的appid");
define("APPSECRET", "您的appsecret ");
 
$token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . APPID . "&secret=" . APPSECRET;
$res = file_get_contents($token_access_url);  //获取文件内容或获取网络请求的内容
//echo $res;
$result = json_decode($res, true);  //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
$access_token = $result[&#39;access_token&#39;];
 
define("ACCESS_TOKEN", $access_token);  //将access_token定义为常量,便于使用.
 
$make_menu_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . ACCESS_TOKEN;
 
$menuData = &#39; {
   "button":[
   {
     "type":"click",
     "name":"今日歌曲",
     "key":"V1001_TODAY_MUSIC"
   },
   {
      "name":"菜单",
      "sub_button":[
      {
        "type":"view",
        "name":"搜索",
        "url":"http://www.soso.com/"
      },
      {
        "type":"view",
        "name":"视频",
        "url":"http://v.qq.com/"
      },
      {
        "type":"click",
        "name":"赞一下我们",
        "key":"V1001_GOOD"
      }]
    }]
 }&#39;;
 
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_URL, $make_menu_url);
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 6.0; Windows NT 5.1; .NET CLR 1.1.4322)");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $menuData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
 
$info = curl_exec($ch);
 
//判读执行过程中是否有错误,有则发送数据错误报告.
if (curl_errno($ch)) {
  echo &#39;Error&#39; . curl_error($ch); //用户检查php运行环境中的curl模块开启情况.
}
 
curl_close($ch);
print_r($info); //查看post提交到微信服务器后,返回的数据.
登入後複製

#自訂選單的取得

<?php
 
define("APPID", "您的appid");
define("APPSECRET", "您的appsecret ");
 
$token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . APPID . "&secret=" . APPSECRET;
$res = file_get_contents($token_access_url);  //获取文件内容或获取网络请求的内容
$result = json_decode($res, true);  //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
$access_token = $result[&#39;access_token&#39;];
 
$make_menu_url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" . $access_token;
 
$menu_json = file_get_contents($make_menu_url);
 
echo $menu_json;
登入後複製

自訂選單的刪除

<?php
 
define("APPID", "您的appid");
define("APPSECRET", "您的appsecret ");
 
$token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . APPID . "&secret=" . APPSECRET;
$res = file_get_contents($token_access_url);  //获取文件内容或获取网络请求的内容
$result = json_decode($res, true);  //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
$access_token = $result[&#39;access_token&#39;];
 
$make_menu_url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" . $access_token;
 
$menu_json = file_get_contents($make_menu_url);
 
echo $menu_json;
登入後複製

以上所述就是本文的全部內容了,希望對大家做微信開發有所幫助。

以上是分享微信開發自訂選單的處理範例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)