微信公衆號的第一次開發,新手爬坑

最近接觸到微信公衆號開發,首先就是爬這個關於自定義菜單的坑了,看了文檔一段時間加上百度的總結了實現的方法
上代碼,這裏要說明我直接用了本身的access——token令牌,這個本身根據本身的令牌來實現。關於appid跟appsercet的獲取就在這裏不解釋了,不會能夠直接百度。下面是簡單實現菜單的方法,本身能夠封裝一些不少的函數,詳情參考微信開發文檔便可。最後附上本身的圖php

<?php
//微信自定義菜單html

header("Content-type: text/html; charset=utf-8");

//define("ACCESS_TOKEN", "5b9FZhqOXvEf2GthK76FF03kio0uUST9uqUT9L3mLli1qKdPp4dF7Ik0ZiKLGlWs4DQHMB-IveCgNvxEuDlMIrqloxrXa1IaYy5cGB3Gp9TvP_Yh-o1H-bi8Nq9BXE3JMAUbADACQK");`

function get_access_token() {json

$appid = "你的微信公衆號 appid";
$appsecret = "你的微信公衆號 appsercet";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$jsoninfo = json_decode($output, true);
$access_token = $jsoninfo["access_token"];
return $access_token;

}api

//建立菜單
function createMenu($data) {安全

$access_token = get_access_token();
var_dump($access_token);
if (!$access_token)
    return;
$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);

}app

//刪除菜單
function deleteMenu() {curl

return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" . ACCESS_TOKEN);

}函數

$data = '{

"button":[
  {
       "name":"安全動態",
       "sub_button":[
       {
           "type":"click",
           "name":"一天一句",
           "key":"V1001_GOOD"
        },
        {
           "type":"click",
           "name":"安全通知",
           "key":"V1001_GOOD"
        },
        {
           "type":"click",
           "name":"重要通知",
           "key":"V1001_GOOD"
        },
        {
           "type":"click",
           "name":"經驗交流",
           "key":"V1001_HELLO_WORLD"
        },
        {
           "type":"click",
           "name":"安全熱點",
           "key":"V1001_GOOD"
        }]
   },
       {
       "name":"小安微課堂",
       "sub_button":[
        {
           "type":"click",
           "name":"學習記錄",
           "key":"V1001_GOOD"
        },
        {
           "type":"click",
           "name":"安全學習",
           "key":"V1001_GOOD"
        },
        {
           "type":"click",
           "name":"本質安全",
           "key":"V1001_HELLO_WORLD"
        },
        {
           "type":"click",
           "name":"班組安全",
           "key":"V1001_GOOD"
        }]
       },
       
      {
       "name":"測試評估",
       "sub_button":[
       {
           "type":"click",
           "name":"聯繫小安",
           "key":"V1001_GOOD"
        },
        {
           "type":"click",
           "name":"投票活動",
           "key":"V1001_GOOD"
        },
        {
           "type":"click",
           "name":"成績查詢",
           "key":"V1001_GOOD"
        },
        {
           "type":"click",
           "name":"考試測評",
           "key":"V1001_HELLO_WORLD"
        },
        {
           "type":"click",
           "name":"用戶登陸",
           "key":"V1001_GOOD"
        }]
   }
   
   ]

}';

echo createMenu($data);
?>

圖片描述

相關文章
相關標籤/搜索