Create/query/cancel php sample code and php sample code in the custom menu of WeChat

Source: Internet
Author: User
Tags php sample code

Create/query/cancel php sample code and php sample code from the custom menu

You can use the custom menu function for Public Account Service numbers. Previously, the menu fails to be created because the $ data format has not been correctly passed and is finally resolved. Here, we first record and encapsulate a class to facilitate the management of custom menus. This class is only a management class for custom menus and does not involve code for custom replies and menu events.

The Code is as follows:

/*** @ Author LSH 2013-09-06 *** create a custom menu | query | cancel */class weixinMenu {public static $ appid = null; // apply for the obtained appid public static $ secret = null; // apply for the obtained secret public static $ getToken = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential "; public static $ createMenu =" https://api.weixin.qq.com/cgi-bin/menu/create? Access_token = "; public static $ selMenu =" https://api.weixin.qq.com/cgi-bin/menu/get? Access_token = "; public static $ delMenu =" https://api.weixin.qq.com/cgi-bin/menu/delete? Access_token = "; public static $ opt = array (CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_USERAGENT => 'mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0 )', CURLOPT_FOLLOWLOCATION => 1, CURLOPT_AUTOREFERER => 1, CURLOPT_RETURNTRANSFER => true); public $ ACCESS_TOKEN = null;/*** create menu */public function create () {$ this-> token (); $ strMeau = '{"button": [{"type": "click", "Name": "menu left", "key": "V_MENU_LEFT" },{ "type": "click", "name": "menu ", "key": "V_MENU_CENTER" },{ "type": "click", "name": "right menu", "key": "V_MENU_RIGHT"}]} '; $ ret = $ this-> HttpPost (self: $ createMenu. $ this-> ACCESS_TOKEN, self: $ opt, $ strMeau); echo $ ret;}/*** query menu */public function sel () {$ this-> token (); $ ret = $ this-> HttpGet (self: $ selMenu. $ this-> ACCESS_TOKEN, self: $ opt); echo $ ret;}/*** cancel the menu */Public function del () {$ this-> token (); $ ret = $ this-> HttpGet (self: $ delMenu. $ this-> ACCESS_TOKEN, self: $ opt); echo $ ret;}/*** get token */private function token () {$ tokenUrl = self :: $ getToken. "& appid = ". self: $ appid. "& secret = ". self: $ secret; $ ret = $ this-> HttpGet ($ tokenUrl, self: $ opt); $ arrRet = json_decode ($ ret, true ); $ this-> ACCESS_TOKEN = $ arrRet ['Access _ token'];}/*** POST mode * @ param s Tring $ url post address * @ param array $ opt post option * @ param array $ post_data post Data * @ return mixed */private function HttpPost ($ url, $ opt = array (), $ post_data) {$ setopt = array (CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $ url, CURLOPT_CUSTOMREQUEST => 'post ', CURLOPT_POST => 1, CURLOPT_POSTFIELDS => $ post_data,); if (! Empty ($ opt) {foreach ($ opt as $ key => $ value) {$ setopt [$ key] = $ value ;}} $ curl = curl_init ($ url); foreach ($ setopt as $ key => $ value) {curl_setopt ($ curl, $ key, $ value );} $ responseText = curl_exec ($ curl); curl_close ($ curl); return $ responseText ;} /*** GET Method * @ param stinrg $ url GET url * @ param array $ opt GET option * @ return mixed */private function HttpGet ($ url, $ opt = array () {$ setop T = array (CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $ url); if (! Empty ($ opt) {foreach ($ opt as $ key => $ value) {$ setopt [$ key] = $ value ;}} $ curl = curl_init ($ url); foreach ($ setopt as $ key => $ value) {curl_setopt ($ curl, $ key, $ value );} $ responseText = curl_exec ($ curl); curl_close ($ curl); return $ responseText ;}}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.