程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php微信開發自定義菜單

php微信開發自定義菜單

編輯:PHP綜合

目前微信服務號自定義菜單最多包括3個一級菜單,每個一級菜單最多包含5個二級菜單。一級菜單最多4個漢字,二級菜單最多7個漢字,多出來的部分將會以“...”代替。請注意,創建自定義菜單後,由於微信客戶端緩存,需要24小時微信客戶端才會展現出來。建議測試時可以嘗試取消關注公眾賬號後再次關注,則可以看到創建後的效果。 

目前自定義菜單接口可實現兩種類型按鈕,如下:
 click:
用戶點擊click類型按鈕後,微信服務器會通過消息接口推送消息類型為event 的結構給開發者(參考消息接口指南),並且帶上按鈕中開發者填寫的key值,開發者可以通過自定義的key值與用戶進行交互;
view:
用戶點擊view類型按鈕後,微信客戶端將會打開開發者在按鈕中填寫的url值 (即網頁鏈接),達到打開網頁的目的,建議與網頁授權獲取用戶基本信息接口結合,獲得用戶的登入個人信息。 

接口調用請求說明
http請求方式:POST(請使用https協議) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN 
請求示例(JSON數據請使用UTF-8編碼)

 {
 "button":[
  {"type":"click","name":"我的信息","sub_button":[
   {"type":"click","name":"拇指查詢","key":"BUTTON_1"},
  {"type":"click","name":"拇指請假","key":"BUTTON_2"},
  {"type":"view","name":"工號綁定","url":"http://XXXXXXXXXXXXXXXXX"}]
 },
  {"type":"click","name":"業務流程","key":"BUTTON_3"},
  {"name":"員工建議","sub_button":[
   {"type":"view","name":"思想火花","url":"http://XXXXXXXXXXXXXXXXXX"},
   {"type":"view","name":"獎品兌換","url":"http://XXXXXXXXXXXXXXXXXX"},
   {"type":"click","name":"贊一下我們","key":"BUTTON_ZAN"}]
  }
 ]
}

參數說明

返回結果 
正確時的返回JSON數據包如下:
 {"errcode":0,"errmsg":"ok"} 
錯誤時的返回JSON數據包如下(示例為無效菜單名長度):
 {"errcode":40018,"errmsg":"invalid button name size"} 

以下是示例代碼(PHP)。        

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta name="author" content="Chris Mao" />
 </head>
 <body>
 <?php
  $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";

  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POST, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

  $output = curl_exec($ch);
  curl_close($ch);
  if (empty($output)) { var_dump($output); exit; }
  $result = json_decode($output);
  $token = $result->access_token;
   
  //創建菜單
  $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$token";
  $jsonData = file_get_contents("menu.json");
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
  curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  $output = curl_exec($ch);
  curl_close($ch); 
  var_dump($output);
 ?>
 </body>
</html>

menu.json 

{
 "button":[
  {"type":"click","name":"我的信息","sub_button":[
   {"type":"click","name":"拇指查詢","key":"BUTTON_1"},
  {"type":"click","name":"拇指請假","key":"BUTTON_2"},
  {"type":"view","name":"工號綁定","url":"http://XXXXXXXXXXXXXXXXX"}]
 },
  {"type":"click","name":"業務流程","key":"BUTTON_3"},
  {"name":"員工建議","sub_button":[
   {"type":"view","name":"思想火花","url":"http://XXXXXXXXXXXXXXXXXX"},
   {"type":"view","name":"獎品兌換","url":"http://XXXXXXXXXXXXXXXXXX"},
   {"type":"click","name":"贊一下我們","key":"BUTTON_ZAN"}]
  }
 ]
}

 響應自定義菜單事件

 $wechatObj = new wechatCallbackAPI();
 if (isset($_GET["echostr"])) { 
  $wechatObj->valid(); 
 } else { 
  $wechatObj->responseMsg();
 }


 class wechatCallbackAPI {

  private $token = "WEIXIN";

  private $appId = "APPID";

  private $appSecret = "APPSECRET";
  
  private function checkSignature() {
   $signature = $_GET["signature"];
   $timestamp = $_GET["timestamp"];
   $nonce = $_GET["nonce"]; 
     
   $tmpArr = array($this->token, $timestamp, $nonce);
   sort($tmpArr);
   $tmpStr = implode($tmpArr);
   $tmpStr = sha1($tmpStr);
   
   if($tmpStr == $signature) {
    return true;
   } else {
    return false;
   }
  }

  private function getAccessToken() {
   $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";

   $ch = curl_init($url);
   $curl_setopt($ch, CURLOPT_HEADER, 0);
   $curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   $curl_setopt($ch, CURLOPT_POST, 0);
   $curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
   $curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

   $output = curl_exec($ch);
   curl_close($ch);
   if (empty($output)) { return ""; }

   $result = json_decode($result);
   return $result->access_token;
  }

  public function valid() {
   $echoStr = $_GET["echostr"];
   
   //valid signature, option
   if($this->checkSignature()){
    echo $echoStr;
    exit;
   }
  }
 
  public function responseMsg() {
   //get post data, May be due to the different environments
   $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
   if (empty($postStr)){
    echo "";
    exit;
   }

   //extract post data
   $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
   $fromUsername = $postObj->FromUserName;
   $toUsername = $postObj->ToUserName;
   $time = time();

   //文本消息模板
   $textTpl = "<xml>
      <ToUserName><![CDATA[%s]]></ToUserName>
      <FromUserName><![CDATA[%s]]></FromUserName>
      <CreateTime>%s</CreateTime>
      <MsgType><![CDATA[%s]]></MsgType>
      <Content><![CDATA[%s]]></Content>
      <FuncFlag>0</FuncFlag>
      </xml>";
   
   switch (strtolower(trim($postObj->MsgType))) {
    case "text": //文本消息
     $keyword = trim($postObj->Content);
     if(!empty($keyword)) {
      $msgType = "text";
      $contentStr = "$fromUsername, 您發送了文本信息: $keyword ";
      if (strtolower($keyword) == "time") {
       $contentStr = date("Y-m-d H:i:s", $time);
      }
      $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
     } else {
      $resultStr = "Input something...";
     }
     break;
    case "image": //圖片消息
     $msgType = "text";
     $contentStr = "$fromUsername, 您發送了圖片信息";
     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
     break;
    case "voice": //聲音消息
     $msgType = "text";
     $contentStr = "$fromUsername, 您發送了聲音信息";
     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
     break;
    case "video": //視頻消息
     $msgType = "text";
     $contentStr = "$fromUsername, 您發送了視頻信息";
     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
     break;
    case "location": //位置消息
     $msgType = "text";
     $contentStr = "$fromUsername, 您發送了位置信息";
     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
     break;
    case "link": //鏈接消息
     $msgType = "text";
     $contentStr = "$fromUsername, 您發送了鏈接信息";
     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
     break;
    case "event": //事件
     switch (strtolower(trim($postObj->Event))) {
      case "subscribe": //關注事件
       $msgType = "text";
       $contentStr = "歡迎您關注XXXXXXX";
       $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
       break;
      case "unsubscribe": //取消關注事件
       break;
      case "scan": //用戶已關注時掃描二維碼事件
       $msgType = "text";
       $contentStr = "$fromUsername, 您掃描了二維碼";
       $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
       break;
      case "location": //上傳地理位置事件
       $msgType = "text";
       $contentStr = "$fromUsername, 您上傳地理位置";
       $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
       break;
      case "click": //自定義菜單事件
       $msgType = "text";
       $contentStr = "$fromUsername, 您點擊了自定義菜單 $postObj->EventKey ";
       if ("BUTTON_ZAN" == $postObj->EventKey) {
        $contentStr = "感謝您的贊,我們會繼續提供更優質的服務。";
       }
       $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
       ;
       break;
      default:
       $resultStr = "";
     }
     break;
    default:
     $resultStr = "";
   }
   echo $resultStr;
  }
 }
?>

自定義菜單查詢 

使用接口創建自定義菜單後,開發者還可使用接口查詢自定義菜單的結構。 

請求說明
 http請求方式:GET
https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN

 返回說明
 對應創建接口,正確的Json返回結果:  復制代碼 代碼如下:{"menu":{"button":[{"name":"我的信息","sub_button":[{"type":"click","name":"拇指查詢","key":"BUTTON_1","sub_button":[]},{"type":"click","name":"拇指請假","key":"BUTTON_2","sub_button":[]},{"type":"view","name":"工號綁定","url":"http:\/\/XXXXXXXX","sub_button":[]}]},{"type":"click","name":"業務流程","key":"BUTTON_3","sub_button":[]},{"name":"員工建議","sub_button":[{"type":"view","name":"思想火花","url":"http:\/\/XXXXXXXX","sub_button":[]},{"type":"view","name":"獎品兌換","url":"http:\/\/XXXXXXXX","sub_button":[]},{"type":"click","name":"贊一下我們","key":"BUTTON_ZAN","sub_button":[]}]}]}}

自定義菜單刪除 

使用接口創建自定義菜單後,開發者還可使用接口刪除當前使用的自定義菜單。 

請求說明
http請求方式:GET
https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN

返回說明
 對應創建接口,正確的Json返回結果:
{"errcode":0,"errmsg":"ok"}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved