php版微信實現公眾號菜單添加刪除操作

來源:互聯網
上載者:User

為了以最快方式調試新菜單功能,就用Debug方式去產生新菜單。請參數微信教程2的 wechat-json類。匯入該檔案後,我們用Debug方式產生一個新菜單:
if (isset($argc)  && $argc >= 1 && $argv[0] == __FILE__) {
    $client = new WechatJSON(array(
        WechatJSON::APP_ID => 'wx78acfe8023sfsd4d51',
        WechatJSON::APP_SECRET => '9ba3476db1ffsfsf512e0b22f630fa',
    ));
 
    $res = $client->call('/menu/create',array (
            'button' => array(
                array (
                    'name' => '掃碼',
                    'sub_button' => array(
                        array(
                            'name' => '掃碼不提示',
                            'type' => 'scancode_push',
                            'key' => 'rselfmenu_0_0',
                            'sub_button' =>array ()
                        ),
                        array(
                            'name' => '掃碼帶提示',
                            'type' => 'scancode_waitmsg',
                            'key' => 'rselfmenu_0_1',
                            'sub_button' =>array ()
                        ),
                    ),
                ),
                array(
                    'name' => '發圖',
                    'sub_button' => array(
                        array(
                            'name' => '系統拍照發圖',
                            'type' => 'pic_sysphoto',
                            'key' => 'rselfmenu_1_0',
                            'sub_button' => array()
                        ),
                        array(
                            'name' => '拍照或者相簿發圖',
                            'type' => 'pic_photo_or_album',
                            'key' => 'rselfmenu_1_1',
                            'sub_button' => array()
                        ),
                        array(
                            'name' => '微信相簿發圖',
                            'type' => 'pic_weixin',
                            'key' => 'rselfmenu_1_2',
                            'sub_button' => array()
                        ),
                    )
                ),
                array(
                    'name' => '發送位置',
                    'type' => 'location_select',
                    'key' => 'rselfmenu_2_0'
                )
            )
        )
        , WechatJSON::JSON);
    if (!$res) {
        var_dump($client->_error);
    }
    var_export($res);
}

執行後,取消追蹤再關注,讓新菜單生效!
效果圖:



測試結果如下:
scancode_push事件:
array (
  'tousername' => 'gh_e2a2b3bd35ff',
  'fromusername' => 'on0eVjnYStxkCSaaCamYCpMZDmwA',
  'createtime' => '1411629272',
  'msgtype' => 'event',
  'event' => 'scancode_push',
  'eventkey' => '6',
  'scancodeinfo' =>
  SimpleXMLElement::__set_state(array(
     'ScanType' => 'qrcode',
     'ScanResult' => 'http://www.baidu.com/',
  )),
array (
  'tousername' => 'gh_e2a2b3bd35ff',
  'fromusername' => 'on0eVjnYStxkCSaaCamYCpMZDmwA',
  'createtime' => '1411629475',
  'msgtype' => 'event',
  'event' => 'scancode_push',
  'eventkey' => '6',
  'scancodeinfo' =>
  SimpleXMLElement::__set_state(array(
     'ScanType' => 'qrcode/EAN_13',
     'ScanResult' => '6925082946487',
     'EventKey' =>
    SimpleXMLElement::__set_state(array(
    )),
  )),
)

 





 


上面是在菜單上點擊《掃碼不提示》後的log,有兩種情況出現,第一種是如果你掃的是二維碼是URL,它就會跳轉到網頁(注包括服務號產生的二維碼),第二種是如果你掃的是條碼,就會跳轉到搜尋到該商品的詳細資料,也就是大家常用的查價格。

scancode_waitmsg事件:
array (
  'tousername' => 'gh_e2a2b3bd35ff',
  'fromusername' => 'on0eVjnYStxkCSaaCamYCpMZDmwA',
  'createtime' => '1411629302',
  'msgtype' => 'event',
  'event' => 'scancode_waitmsg',
  'eventkey' => '6',
  'scancodeinfo' =>
  SimpleXMLElement::__set_state(array(
     'ScanType' => 'qrcode',
     'ScanResult' => 'http://www.111cn.net/',
  )),

 


上面是點擊菜單《掃碼提示》後的log,推送XML跟scancode_push時差不多,但它不會跳轉到網址或者商品資訊。博主認為,這有利於後台取得scancodeinfo的資訊來進一步處理!打個比方,自己自訂二維碼資訊,然後截取處理。類似於原服務號的參數二維碼。
注意,以上兩個菜單掃描事件和微信APP的掃一掃,是有區別的。具體你看事件就能看出來,scan事件!
pic_sysphoto事件、pic_photo_or_album事件和pic_weixin事件
array (
  'tousername' => 'gh_e2a2b3bd35ff',
  'fromusername' => 'on0eVjnYStxkCSaaCamYCpMZDmwA',
  'createtime' => '1411627313',
  'msgtype' => 'image',
  'picurl' => 'http://mmbiz.qpic.cn/mmbiz/L8zbjcLqNFvEZ4dne4MGQQGR8xuHk4KhEk3icghU6a4bFTXnP2oeicr5VaBVJa10w4MYOOEia4udqicT5fdtAADHYg/0',
  'msgid' => '6062893143676022221',
  'mediaid' => 'i7hYOlSXbUCaC7Z9Elx4WpBqQq37-hR0El5w-frPfD5WCdBC7x46DPO6HL7zMfgd',
)

 





 


上面是點《發圖》後,三個子功能表選擇或者拍好,發圖後的log資訊,從圖中我們看到事件是推過來了image,其他資訊,相信大家都知道是什麼,不解釋。
location_select事件:
array (
  'tousername' => 'gh_e2a2b3bd35ff',
  'fromusername' => 'on0eVjnYStxkCSaaCamYCpMZDmwA',
  'createtime' => '1411627424',
  'msgtype' => 'event',
  'event' => 'location_select',
  'eventkey' => '6',
  'sendlocationinfo' =>
  SimpleXMLElement::__set_state(array(
     'Location_X' => '23',
     'Location_Y' => '113',
     'Scale' => '15',
     'Label' =>
    SimpleXMLElement::__set_state(array(
    )),
     'Poiname' =>
    SimpleXMLElement::__set_state(array(
    )),
  )),
)






上面是點擊菜單《發送位置》後的log,我們可以看出事件是location_select,不像發圖那樣變成image,最有價值的資訊是我們要取的sendlocationinfo裡的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.