QR code development and menu creation on WeChat public platform

Source: Internet
Author: User
This article introduces the QR code development, menu creation, and QR code for the public platform.

1. the temporary QR code has an expiration time. it can be set to expire 7 days (604800 seconds) after the QR code is generated, but a large number of temporary QR codes can be generated. Temporary QR codes are mainly used for account binding and other business scenarios that do not require permanent saving of QR codes.
2. permanent QR codes have no expiration time, but the number is small (up to 0.1 million at present ). Permanent QR codes are mainly used for account binding, user source statistics, and other scenarios. The maximum value of a permanent QR code is 100000 (currently only 1--100000 is supported ).

The following code is an example of obtaining a permanent QR code:


/*** Obtain the required ticket * @ param $ access_token * @ param $ sceneId scene value id * @ return bool | mixed */public function getQrticket ($ access_token, $ sceneId) {$ url = 'https: // api.weixin.qq.com/cgi-bin/qrcode/create? Access_token = '. $ access_token; $ raw = array ('Action _ name' => 'QR _ LIMIT_SCENE ', 'action _ info' => array ('scene' => array ('scene _ id' => $ sceneId); $ json = json_encode ($ raw ); $ res = $ this-> rawpost ($ url, $ json); if ($ res = false) {return false;} $ qrres = json_decode ($ res, true); if (isset ($ qrres ['errcode']) {return false;} return $ qrres ;}


The returned JSON will be shown in:


The URL in this JSON is the content of the QR code, and the next step is to convert the URL into a QR code. for high endpoints, you can use the code. I am so lazy that I can directly create a QR code online.

If I scan the QR code, different messages will be pushed. The following is some code:

If (isset ($ msgObj-> EventKey) & preg_match ('/qrscene _(. *)/', $ msgObj-> EventKey, $ scene) {// scan the code to follow switch ($ scene [1]) {case AppConst: SCENE_ALCOHOL: $ pushData ['title'] = 'title'; break;} $ pushData ['picurl'] = 'http: // mmbiz.qpic.cn '; $ pushData ['description'] = 'Description! '; $ PushData ['URL'] = 'http: // mp.weixin.qq.com'; $ msg = $ weixin-> createRawTuWenMsg ($ fromUserName, $ openId, array ($ pushData ));}
II. create a menu

1. a custom menu can contain up to three level-1 menus, and each level-1 menu can contain up to five level-2 menus.

2. a level-1 menu can contain up to four Chinese characters, and a level-2 menu can contain up to seven Chinese characters. the Extra content will be replaced.

3. after creating a custom menu, it takes 24 hours for the client to be displayed due to client cache.

During the test, you can try to remove the public account and then follow it again. then you can see the created effect. Click to view online documents.

The custom menu interface can implement multiple types of buttons. here I will introduce two types, as shown below:

1. click: click to push events

After you click the click type button, the server pushes the structure of the message type event to the developer (reference Message Interface Guide) through the message interface, and carries the key value filled by the developer in the button, developers can interact with users through custom key values;

2. view: jump URL

After you click the view type button, the client will open the webpage URL filled in by the developer in the button, which can be combined with the webpage authorization interface to obtain basic user information.

Public function actionCreatemenu () {$ token = $ this-> get ('token'); $ innerWeixinKey = WEIXIN_INNER_KEY; $ trueToken = md5 ('menu '. $ innerWeixinKey. 'menu '); if ($ token! = $ TrueToken) {$ this-> innerOutput (1, 'request authentication');} $ weixin = new Weixin (); // Please note that the Chinese character in the menu must adopt a certain encoding $ menu = array ('Button '=> array ('name' => $ this-> encodeZH (' menu 1 '), 'Sub _ click' => array ('type' => 'view', 'name' => $ this-> encodeZH ('subfoly '), 'key' => 'menu _ GENE_INDEX ', 'URL' =>' http://mp.weixin.qq.com /'), Array ('type' => 'view', 'name' => $ this-> encodeZH ('interesting top '), 'key' => 'menu _ article', 'URL' =>' http://mp.weixin.qq.com '), Array ('type' => 'view', 'name' => $ this-> encodeZH ('product Preview '), 'key' => 'menu _ product', 'URL' =>' http://mp.weixin.qq.com /'), Array ('name' => $ this-> encodeZH ('product process '), 'Sub _ callback' => array ('type' => 'view', 'name' => $ this-> encodeZH ('wusong Tigers '), 'key' => 'menu _ game', 'URL' => 'xxxx'), array ('type' => 'view ', 'name' => $ this-> encodeZH ('product details'), 'key' => 'menu _ PRODUCT_DETAIL ', 'URL' =>' http://mp.weixin.qq.com /'); $ MenuMsg = $ this-> decodeZHMsg (json_encode ($ menu); $ accessMongo = new WeixinAccessToken (); $ access_token = $ accessMongo-> getAccessToken (); if (! $ Access_token) {$ this-> innerOutput (2, 'access_token' cannot be obtained);} $ weixin = new Weixin (); $ res = $ weixin-> customMenu ($ menuMsg, $ access_token); if (! $ Res) {$ this-> innerOutput (3, 'menu creation failed');} $ this-> innerOutput (0, 'menu created successfully ');}

1. WEIXIN_INNER_KEY is used to perform a simple verification at the beginning of the code.

2. the structure of the Start menu must be name, type, etc.

3. get the common token, which can be obtained by referring to the access_token and log development methods on the public platform.

4. Note that the above code contains the encodeZH and decodeZHMsg methods for encoding.

/*** Encode a Chinese string * @ param $ name * @ return string */private function encodeZH ($ name) {return '[@'. base64_encode ($ name ). '@]';}/*** decodes a Chinese-encoded string in the message * @ param $ msg * @ return mixed */private function decodeZHMsg ($ msg) {return preg_replace_callback ('/\[\@(. + ?) \@\]/', Function ($ match) {return base64_decode ($ match [1]) ;}, $ msg );}

Download demo:

Github address: https://github.com/pwstrick/weixin_demo

CSDN address: http://download.csdn.net/detail/loneleaf1/9045731

For more articles about QR code development and menu creation on the public platform, refer to PHP Chinese website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.