微信菜單事件推送

用戶點擊菜單以後,微信會將事件推送給接口程序,相應的接口參數及說明git

用戶點擊自定義菜單後,接口程序收到的XML數據包以下所示:微信

<xml>
    <ToUserName><![CDATA[gh_36dd0f0b3132]]></ToUserName>
    <FromUserName><![CDATA[oQW8FuN_DFnKZwBaUKQ1RJD2Tr9M]]></FromUserName>
    <CreateTime>1468050882</CreateTime>
    <MsgType><![CDATA[event]]></MsgType>
    <Event><![CDATA[CLICK]]></Event>
    <EventKey><![CDATA[TEXT]]></EventKey>
</xml>

按鈕類型爲view的菜單點擊後,上報的XML以下所示:this

<xml>
    <ToUserName><![CDATA[gh_36dd0f0b3132]]></ToUserName>
    <FromUserName><![CDATA[oQW8FuN_DFnKZwBaUKQ1RJD2Tr9M]]></FromUserName>
    <CreateTime>1468050934</CreateTime>
    <MsgType><![CDATA[event]]></MsgType>
    <Event><![CDATA[VIEW]]></Event>
    <EventKey><![CDATA[http://xw.qq.com/]]></EventKey>
    <MenuId>410418124</MenuId>
</xml>

點擊scancode_push類型的菜單時,接口程序收到的XML數據包以下所示。
  微信會直接運行解碼後的內容,好比直接進入關注界面。spa

<xml>
    <ToUserName><![CDATA[gh_36dd0f0b3132]]></ToUserName>
    <FromUserName><![CDATA[oQW8FuN_DFnKZwBaUKQ1RJD2Tr9M]]></FromUserName>
    <CreateTime>1468051082</CreateTime>
    <MsgType><![CDATA[event]]></MsgType>
    <Event><![CDATA[scancode_push]]></Event>
    <EventKey><![CDATA[rselfmenu_2_2]]></EventKey>
    <ScanCodeInfo>
        <ScanType><![CDATA[qrcode]]></ScanType>
        <ScanResult><![CDATA[http://weixin.qq.com/r/l0Ozq9-EbYISrZvI9xaF]]></ScanResult>
    </ScanCodeInfo>
</xml>

點擊scancode_waitmsg類型的菜單時,接口程序收到的XML數據包以下所示:code

<xml>
    <ToUserName><![CDATA[gh_36dd0f0b3132]]></ToUserName>
    <FromUserName><![CDATA[oQW8FuN_DFnKZwBaUKQ1RJD2Tr9M]]></FromUserName>
    <CreateTime>1468051112</CreateTime>
    <MsgType><![CDATA[event]]></MsgType>
    <Event><![CDATA[scancode_waitmsg]]></Event>
    <EventKey><![CDATA[rselfmenu_2_1]]></EventKey>
    <ScanCodeInfo>
        <ScanType><![CDATA[qrcode]]></ScanType>
        <ScanResult><![CDATA[http://weixin.qq.com/r/l0Ozq9-EbYISrZvI9xaF]]></ScanResult>
    </ScanCodeInfo>
</xml>

點擊pic_sysphoto類型的菜單後,調用手機裏面的照相機功能,而照相之後再發過來時,就收到一個圖片消息。點擊菜單時的XML數據以下所示:xml

<xml>
    <ToUserName><![CDATA[gh_36dd0f0b3132]]></ToUserName>
    <FromUserName><![CDATA[oQW8FuNH_V1zk4x3zTN1IMoAZDW0]]></FromUserName>
    <CreateTime>1468051307</CreateTime>
    <MsgType><![CDATA[event]]></MsgType>
    <Event><![CDATA[pic_sysphoto]]></Event>
    <EventKey><![CDATA[rselfmenu_2_3]]></EventKey>
    <SendPicsInfo>
        <Count>1</Count>
        <PicList>
            <item>
                <PicMd5Sum><![CDATA[4cc7f5ab2c499b01655e99a868ef3519]]></PicMd5Sum>
            </item>
        </PicList>
    </SendPicsInfo>
</xml>

點擊pic_photo_or_album類型的菜單以後,先推送菜單事件推送給開發者,而後再推送圖片消息,點擊菜單時的XML數據以下所示:blog

<xml>
    <ToUserName><![CDATA[gh_36dd0f0b3132]]></ToUserName>
    <FromUserName><![CDATA[oQW8FuNH_V1zk4x3zTN1IMoAZDW0]]></FromUserName>
    <CreateTime>1468051528</CreateTime>
    <MsgType><![CDATA[event]]></MsgType>
    <Event><![CDATA[pic_photo_or_album]]></Event>
    <EventKey><![CDATA[rselfmenu_2_4]]></EventKey>
    <SendPicsInfo>
        <Count>2</Count>
        <PicList>
            <item>
                <PicMd5Sum><![CDATA[b5c23feac0987db7ddda1b0a3addba9d]]></PicMd5Sum>
            </item>
            <item>
                <PicMd5Sum><![CDATA[59c3eb7e2124a75986295b8842573951]]></PicMd5Sum>
            </item>
        </PicList>
    </SendPicsInfo>
</xml>

點擊pic_weixin類型的菜單時,微信客戶端將調起系統相機,用戶能夠選擇已有相片或者進行拍照,微信會將相片發送給開發者,下面是一次選擇3張相片時的XML數據:接口

<xml>
    <ToUserName><![CDATA[gh_36dd0f0b3132]]></ToUserName>
    <FromUserName><![CDATA[oQW8FuNH_V1zk4x3zTN1IMoAZDW0]]></FromUserName>
    <CreateTime>1468051592</CreateTime>
    <MsgType><![CDATA[event]]></MsgType>
    <Event><![CDATA[pic_weixin]]></Event>
    <EventKey><![CDATA[rselfmenu_2_5]]></EventKey>
    <SendPicsInfo>
        <Count>3</Count>
        <PicList>
            <item>
                <PicMd5Sum><![CDATA[59c3eb7e2124a75986295b8842573951]]></PicMd5Sum>
            </item>
            <item>
                <PicMd5Sum><![CDATA[b5c23feac0987db7ddda1b0a3addba9d]]></PicMd5Sum>
            </item>
            <item>
                <PicMd5Sum><![CDATA[4cc7f5ab2c499b01655e99a868ef3519]]></PicMd5Sum>
            </item>
        </PicList>
    </SendPicsInfo>
</xml>

點擊location_select類型的菜單以後,將會調起發送位置功能,在用戶發送位置以後,會再推送一個地理位置消息功能給用戶, XML數據以下所示。事件

<xml>
    <ToUserName><![CDATA[gh_36dd0f0b3132]]></ToUserName>
    <FromUserName><![CDATA[oQW8FuNH_V1zk4x3zTN1IMoAZDW0]]></FromUserName>
    <CreateTime>1468051658</CreateTime>
    <MsgType><![CDATA[event]]></MsgType>
    <Event><![CDATA[location_select]]></Event>
    <EventKey><![CDATA[SIGNIN]]></EventKey>
    <SendLocationInfo>
        <Location_X><![CDATA[22.53996467590332]]></Location_X>
        <Location_Y><![CDATA[113.93487548828125]]></Location_Y>
        <Scale><![CDATA[17]]></Scale>
        <Label><![CDATA[廣東省深圳市南山區深南大道10000號]]></Label>
        <Poiname><![CDATA[騰訊大廈]]></Poiname>
    </SendLocationInfo>
</xml>

消息接口中,響應自定義菜單點擊事件的核心代碼以下所示:圖片

//接收事件消息
private function receiveEvent($object)
{
    $content = "";
    switch ($object->Event)
    {
        case "subscribe":
            $content = "歡迎關注方倍工做室 ";
            $content .= (!empty($object->EventKey))?("\n來自二維碼場景 ".str_replace("qrscene_","",$object->EventKey)):"";
            break;
        case "unsubscribe":
            $content = "取消關注";
            break;
        case "CLICK":
            switch ($object->EventKey)
            {
                case "COMPANY":
                    $content = array();
                    $content[] = array("Title"=>"方倍工做室", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
                    break;
                default:
                    $content = "點擊菜單:".$object->EventKey;
                    break;
            }
            break;
        case "VIEW":
            $content = "跳轉連接 ".$object->EventKey;
            break;
        case "SCAN":
            $content = "掃描場景 ".$object->EventKey;
            break;
        case "LOCATION":
            $content = "上傳位置:緯度 ".$object->Latitude.";經度 ".$object->Longitude;
            break;
        case "scancode_waitmsg":
            $content = "掃碼帶提示:類型 ".$object->ScanCodeInfo->ScanType." 結果:".$object->ScanCodeInfo->ScanResult;
            break;
        case "scancode_push":
            $content = "掃碼推事件";
            break;
        case "pic_sysphoto":
            $content = "系統拍照";
            break;
        case "pic_weixin":
            $content = "相冊發圖:數量 ".$object->SendPicsInfo->Count;
            break;
        case "pic_photo_or_album":
            $content = "拍照或者相冊:數量 ".$object->SendPicsInfo->Count;
            break;
        case "location_select":
            $content = "發送位置:標籤 ".$object->SendLocationInfo->Label;
            break;
        default:
            $content = "receive a new event: ".$object->Event." \n技術支持 方倍工做室";
            break;
    }

    if(is_array($content)){
        if (isset($content[0]['PicUrl'])){
            $result = $this->transmitNews($object, $content);
        }else if (isset($content['MusicUrl'])){
            $result = $this->transmitMusic($object, $content);
        }
    }else{
        $result = $this->transmitText($object, $content);
    }
    return $result;
}
相關文章
相關標籤/搜索