php微信開發(fā)自定義菜單
目前微信服務(wù)號自定義菜單最多包括3個一級菜單,每個一級菜單最多包含5個二級菜單。一級菜單最多4個漢字,二級菜單最多7個漢字,多出來的部分將會以“...”代替。請注意,創(chuàng)建自定義菜單后,由于微信客戶端緩存,需要24小時微信客戶端才會展現(xiàn)出來。建議測試時可以嘗試取消關(guān)注公眾賬號后再次關(guān)注,則可以看到創(chuàng)建后的效果。
目前自定義菜單接口可實現(xiàn)兩種類型按鈕,如下:
click:
用戶點(diǎn)擊click類型按鈕后,微信服務(wù)器會通過消息接口推送消息類型為event 的結(jié)構(gòu)給開發(fā)者(參考消息接口指南),并且?guī)习粹o中開發(fā)者填寫的key值,開發(fā)者可以通過自定義的key值與用戶進(jìn)行交互;
view:
用戶點(diǎn)擊view類型按鈕后,微信客戶端將會打開開發(fā)者在按鈕中填寫的url值 (即網(wǎng)頁鏈接),達(dá)到打開網(wǎng)頁的目的,建議與網(wǎng)頁授權(quán)獲取用戶基本信息接口結(jié)合,獲得用戶的登入個人信息。
接口調(diào)用請求說明
http請求方式:POST(請使用https協(xié)議) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
請求示例(JSON數(shù)據(jù)請使用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":"業(yè)務(wù)流程","key":"BUTTON_3"},
{"name":"員工建議","sub_button":[
{"type":"view","name":"思想火花","url":"http://XXXXXXXXXXXXXXXXXX"},
{"type":"view","name":"獎品兌換","url":"http://XXXXXXXXXXXXXXXXXX"},
{"type":"click","name":"贊一下我們","key":"BUTTON_ZAN"}]
}
]
}
參數(shù)說明

返回結(jié)果
正確時的返回JSON數(shù)據(jù)包如下:
{"errcode":0,"errmsg":"ok"}
錯誤時的返回JSON數(shù)據(jù)包如下(示例為無效菜單名長度):
{"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;
//創(chuàng)建菜單
$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":"業(yè)務(wù)流程","key":"BUTTON_3"},
{"name":"員工建議","sub_button":[
{"type":"view","name":"思想火花","url":"http://XXXXXXXXXXXXXXXXXX"},
{"type":"view","name":"獎品兌換","url":"http://XXXXXXXXXXXXXXXXXX"},
{"type":"click","name":"贊一下我們","key":"BUTTON_ZAN"}]
}
]
}
響應(yīng)自定義菜單事件
$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, 您發(fā)送了文本信息: $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, 您發(fā)送了圖片信息";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
break;
case "voice": //聲音消息
$msgType = "text";
$contentStr = "$fromUsername, 您發(fā)送了聲音信息";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
break;
case "video": //視頻消息
$msgType = "text";
$contentStr = "$fromUsername, 您發(fā)送了視頻信息";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
break;
case "location": //位置消息
$msgType = "text";
$contentStr = "$fromUsername, 您發(fā)送了位置信息";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
break;
case "link": //鏈接消息
$msgType = "text";
$contentStr = "$fromUsername, 您發(fā)送了鏈接信息";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
break;
case "event": //事件
switch (strtolower(trim($postObj->Event))) {
case "subscribe": //關(guān)注事件
$msgType = "text";
$contentStr = "歡迎您關(guān)注XXXXXXX";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
break;
case "unsubscribe": //取消關(guān)注事件
break;
case "scan": //用戶已關(guān)注時掃描二維碼事件
$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, 您點(diǎn)擊了自定義菜單 $postObj->EventKey ";
if ("BUTTON_ZAN" == $postObj->EventKey) {
$contentStr = "感謝您的贊,我們會繼續(xù)提供更優(yōu)質(zhì)的服務(wù)。";
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
;
break;
default:
$resultStr = "";
}
break;
default:
$resultStr = "";
}
echo $resultStr;
}
}
?>
自定義菜單查詢
使用接口創(chuàng)建自定義菜單后,開發(fā)者還可使用接口查詢自定義菜單的結(jié)構(gòu)。
請求說明
http請求方式:GET
https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN
返回說明
對應(yīng)創(chuàng)建接口,正確的Json返回結(jié)果:
自定義菜單刪除
使用接口創(chuàng)建自定義菜單后,開發(fā)者還可使用接口刪除當(dāng)前使用的自定義菜單。
請求說明
http請求方式:GET
https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN
返回說明
對應(yīng)創(chuàng)建接口,正確的Json返回結(jié)果:
{"errcode":0,"errmsg":"ok"}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
PHP根據(jù)兩點(diǎn)間的經(jīng)緯度計算距離
這篇文章主要介紹了PHP如何根據(jù)兩點(diǎn)間的經(jīng)緯度計算距離,代碼很簡單,但很實用,需要的朋友可以參考下2014-10-10
PHP中使用php://input處理相同name值的表單數(shù)據(jù)
這篇文章主要介紹了PHP中使用php://input處理相同name值的表單數(shù)據(jù),本文是另一種處理相同name值表單數(shù)據(jù)的方法,文中同時給出另一種方法,需要的朋友可以參考下2015-02-02

