WeChat/Forex Public Platform Development (2): PHP Implementation of custom menus (source code provided)

Source: Internet
Author: User
/Forex Public Platform Development (II): PHP Implementation of custom menus (source code provided)

Divide the public number into subscription number and service number two, service number can customize the menu, the menu greatly facilitates user operation.

For example: The public service number "China Southern Airlines" custom menu such as:

A la carte can be directly into the operation, convenient!

PS: Service number needs to be registered as a unit (need to upload unit ID, etc. for authentication), personal identity can only register the subscription number (no custom menu)

PS: Easy letter allows all public numbers can be customized menu (or easy to believe good!) )

However, for public platform developers, it's a bit of a hassle to define and build menus.

I read the development document and tested it for 3 hours before I figured it out. Here, write some ideas and provide a class that completely simplifies the coding work of the developer.

Let's talk about the principle (see the Common Platform development documentation):

1, register the public number, open the developer mode, the platform will provide two parameters Appid,appsecret (for the subscription number, the platform does not provide; Easy to trust all public numbers are available)

2, before customizing the menu, you must request a voucher (Accesstoken) from the platform as follows:

Read URL https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret= with get Appsecret

Where: Appid,appsecret is used to substitute actual parameter values

The returned result is a JSON-formatted text with Accesstoken. (JSON is a data interchange format, students do not know to learn from the beginning to understand)

Accesstoken is not permanently valid, the return result has a failure time, that is, after xx seconds (usually a day or so), Accesstoken will be invalidated.

For the easy-to-trust platform, the above URL is https://api.yixin.im/cgi-bin/token?grant_type=client_credential&appid=APPID&secret= Appsecret

3, after obtaining the valid Accesstoken, can make the custom menu creation, the deletion operation.

3.1 Creating a Menu

Submit the menu definition data to this URL by post, Url:https://api.weixin.qq.com/cgi-bin/menu/create?access_token=access_token

Where:access_token is used to substitute actual parameter values

The menu definition data is a JSON-formatted text (the development document is not so clear, let me understand it for a while), as the Post method of the submission data

The returned result is a JSON-formatted text with an operation success code and an error message

For the easy-to-trust platform, the URL to create the menu is Https://api.yixin.im/cgi-bin/menu/create?access_token=ACCESS_TOKEN

3.2 Delete a menu

Read URL Https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN with get

Where:access_token is used to substitute actual parameter values

The returned result is a JSON-formatted text with Accesstoken. (JSON is a data interchange format, students do not know to learn from the beginning to understand)

Accesstoken is not permanently valid, the returned result has a failure time, that is, after xx seconds (usually a day or so), the Accesstoken will fail.

For the easy-to-trust platform, the above URL is Https://api.yixin.im/cgi-bin/menu/delete?access_token=ACCESS_TOKEN

This process requires developers to understand the HTTP protocol details and JSON format, which can be a nightmare for a typical developer.

I coded, tested and offered two classes to completely simplify the development of custom menus for developers (developers no longer have to understand HTTP protocols, JSON, and common platform protocols)

The results are as follows:

Development language: PHP 5.X

Source code Download Address: http://download.csdn.net/detail/c80486/6357873

File name: jostudio.wechatmenu.php It's only used here. (Interested students, you can read the source code comments)

There are two classes defined in the file:

First class: Wechatmenu for menu operations

Second class: Menudefine for menu data definition

Custom menu actions implemented with these two classes, routines file test_menu.php

Include_once ' jostudio.wechatmenu.php '; Contains the Wechatmenu class

$AppId = "9cXXXXXXXXXXXXXXXXXX"; AppID parameters provided by the public platform
$AppSecret = "61XXXXXXXXXXXXXX"; Appsecret parameters provided by the public platform

Create an instance of the Wechatmenu class
$object = new Wechatmenu ("Weixin", $AppId, $AppSecret); The first parameter, "Weixin", indicates a platform-specific
$object = new Wechatmenu ("Yixin", $AppId, $AppSecret); The first parameter, "Yixin", indicates that it is for an easy-to-trust platform

Define a menu data
$menu = new Menudefine (); Create a Menudefine instance

$menu->menustart (); Menu Start

$menu->addmenu ("entertainment world");
$menu->addmenuitem ("Guessing", "riddle");
$menu->addmenuitem ("Telling jokes", "joke");
$menu->addmenuitem ("Listen to Music", "musical");
$menu->addmenuitem ("See the movie", "movie");
$menu->addmenuitem ("read the novel", "novel");

$menu->addmenu ("utility");
$menu->addmenuitem ("Find food", "foods");
$menu->addmenuitem ("City Weather", "weather");
$menu->addmenuitem ("translation", "translate");

$menu->menuend (); When the menu definition ends, there is a menu definition data in $MENU->STR (JSON format)

Build Menu
echo "

Create Menu

";
if ($object->createmenu ($menu->str))//$menu->STR with menu definition data (JSON format)
echo "Create menu OK";
Else
echo "Create menu failure:". $menuObject->errmsg;
echo "";


Get current Menu Data
echo "

Get menu:the Menu JSON data is

";
echo $object->getmenu ();
echo "";

/*
Delete Menu
echo "

Delete Menu

";
echo $object->deletemenu ();
echo "";
*/

?>

Code Description:

1, first: include_once ' jostudio.wechatmenu.php '; Contains the Wechatmenu class

2, $AppId, $AppSecret is the two parameters provided by the platform, please modify the code, fill in the real value

3, create an instance of the Wechatmenu class

$object = new Wechatmenu ($platform, $AppId, $AppSecret);

The first parameter (text type) $platform indicate which platform is targeted, platform is "Weixin", and "Yixin" is the easy-to-trust platform.

4. Define Menu Data

4.1 First, create a Menudefine instance

$menu = new Menudefine (); Create a Menudefine instance

4.2 Then join the menu

$menu->addmenu ($name); First level menu, $name for dish sole name

4.3 Re-join menu item

$menu->addmenuitem ($name, $key); Second Level menu

$name as the menu item name

$key is the key value of the menu, when the user taps the menu item, a click message is generated that has the key value indicating which menu was clicked.

4.4 And so on, join one by one.

A total of two level menus are defined in the above routines

4.5 Menu definition end, with $menu->menuend (); Ends the menu definition, the menu definition data (JSON format) has been generated in the $menu str variable

Menudefine This class is used to simplify the menu definition, and the last generated JSON-formatted data is saved in the $menu->str.

5. Call the CreateMenu ($menu _data) method of the Wechatmenu class to create a custom menu

Code: $object->createmenu ($menu->str))

CreateMenu () will automatically complete all the procedures in the introduction of the principle

If the menu is created successfully, CreateMenu () returns True

If the menu fails to be created, CreateMenu () returns false, error codes and error messages are recorded in $object->errcode and $object->errmsg two variables

6, call the GetMenu () method of the Wechatmenu class to read the menu definition data on the current platform and return the result as a JSON-formatted text

7, call the DeleteMenu () method of the Wechatmenu class to delete the menu definition on the platform, and return TRUE if successful

test_menu.php This routine file is fully available, modify it as needed, upload to the server, load to complete the custom menu operation

The actual screen effect of the above menu is as follows:

This is my easy-to-believe public number menu effect.

Since my public number is not a service number, but a subscription number, there is no custom menu function, I can only complete this menu in the easy letter.

You can scan the following two-dimensional code in the easy letter, add the easy letter public number "intelligent technology", actually look at the menu effect

  • 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.