1菩彬、獲取access_token
2缠劝、獲取圖文消息media_id
3、確定文本回復(fù)key骗灶,構(gòu)建菜單json字符串
4惨恭、請(qǐng)求菜單創(chuàng)建接口
1、獲取access_token
方便起見(jiàn)耙旦,用微信公眾平臺(tái)接口調(diào)試工具(http://mp.weixin.qq.com/debug?token=1606511159&lang=zh_CN)獲取access_token
獲取過(guò)程可能會(huì)遇到白名單問(wèn)題脱羡,在公眾號(hào)開(kāi)發(fā)基本配置中臨時(shí)修改下白名單,不出意外access_token獲取成功
2、因菜單包含圖文消息菜單轻黑,需獲取圖文消息media_id,方便起見(jiàn)利用postman調(diào)用接口獲取琴昆。(上述第一步獲取access_token也可以直接用postman)
調(diào)試公眾號(hào)POST請(qǐng)求(獲得后臺(tái)素材media_id)
接口地址
http請(qǐng)求方式: POST
https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN
參數(shù)說(shuō)明
參數(shù) | 是否必須 | 說(shuō)明 |
---|---|---|
type | 是 | 素材的類型氓鄙,圖片(image)、視頻(video)业舍、語(yǔ)音 (voice)抖拦、圖文(news) |
offset | 是 | 從全部素材的該偏移位置開(kāi)始返回,0表示從第一個(gè)素材 返回 |
count | 是 | 返回素材的數(shù)量舷暮,取值在1到20之間 |
將剛剛獲得的基礎(chǔ)token拼入接口地址填入postman 選擇請(qǐng)求方式為POST
添加請(qǐng)求參數(shù)
返回結(jié)果中獲取media_id
3态罪、包含直接回復(fù)文本的菜單,確定key,構(gòu)建菜單json字符串
$jsonMenu = '
{
"button": [
{
"name": "資源管理",
"sub_button": [
{
"type": "view",
"name": "我的記錄",
"url": "http://m.test.net/h5/my/index",
"sub_button": [ ]
},
{
"type": "click",
"name": "使用說(shuō)明1",
"key": "SYSM-001",
"sub_button": [ ]
},
{
"type": "click",
"name": "使用說(shuō)明2",
"key": "SYSM-002",
"sub_button": [ ]
}
]
},
{
"name": "加盟培訓(xùn)",
"sub_button": [
{
"type": "view",
"name": "安全通告",
"url": "http://mp.weixin.qq.com/s/haHruj5RgoawpYOoEUA1CA",
"sub_button": [ ]
},
{
"type": "view_limited",
"name": "圖文消息1",
"media_id": "O4NvReZyZPUCT4efE3q6FwtfaTmWyEgIbLg2arIGiA0"
},
{
"type": "view_limited",
"name": "圖文消息2",
"media_id": "O4NvReZyZPUCTyefE3q6FwlTyoldJsDiHbFKCwdmPSc"
},
]
},
]
}';
文本回復(fù)的菜單需在代碼中作相應(yīng)處理下面,檢測(cè)event中click,匹配key值复颈,回復(fù)對(duì)應(yīng)文本消息內(nèi)容
public function weixin(){
$postStr = $this->input->raw_input_stream;
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
if(!empty($postObj)){
$MsgType = $postObj->MsgType;
switch($MsgType){
case "text":
$resultStr = $this->_handle_text($postObj);
break;
case "event":
$resultStr = $this->_handle_event($postObj);
break;
default:
$resultStr = "Unknow msg type: ".$MsgType;
break;
}
echo $resultStr;
}
else{
echo 'error';
}
}
function _handle_event($postObj){
$openid = strval($postObj->FromUserName);
$EventKey = strval($postObj->EventKey);
ll('Weixinlib');
$userinfo = $this->weixinlib->get_user_info($openid); // 獲取平臺(tái)用戶信息 array
$msgType = 'text';
switch (strtolower($postObj->Event))
{
case "subscribe":
$content = $this->_handle_event_subscribe($postObj,$userinfo);
break;
case "scan":
$content = $this->_handle_event_scan($postObj,$userinfo);
break;
case "click"://文本消息回復(fù)處理
$content = $this->_handle_event_click($postObj);
break;
default :
$content = "Unknow Event: ".$postObj->Event;
break;
}
$resultStr = $this->_response($postObj, $content,$msgType);
return $resultStr;
}
function _handle_event_click($postObj){
$scene = $postObj->EventKey;
return $this->_click_handle_scene($scene);
}
function _click_handle_scene($scene){
switch ($scene){
case 'SYSM-001':
$resultStr = "SYSM-001對(duì)應(yīng)的文本消息";
break;
case 'SYSM-002':
$resultStr = "SYSM-001對(duì)應(yīng)的文本消息";
break;
default:
$resultStr = "unknow click";
break;
}
return $resultStr;
}
function _response($object, $content,$msgType = 'text'){
if(empty($content)){
return;
}
if($msgType == 'text'){
return $this->_response_text($object, $content);
}else if($msgType == 'news'){ //圖文消息
return $this->_response_news($object, $content);
}
}
private function _response_text($object, $content, $flag=0)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%d</FuncFlag>
</xml>";
$resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
return $resultStr;
}
調(diào)用創(chuàng)建菜單接口,創(chuàng)建菜單
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$access_token";
$output = http_post($url, $jsonMenu);
成功返回:{"errcode":0,"errmsg":"ok"}