WeChat Public Platform custom menu _php Tutorial

Source: Internet
Author: User
Recently the public platform is very hot, certainly a lot of companies like our company, to develop a public platform, here to record the production of custom menus encountered problems,

Hope to give some people some ideas, or a little detour.

In order to modify the custom menu dynamically, the menu item is stored in the database $arr is the processed array format that is queried from the database:

Array (1) {["button"] = = Array (3) {[0] = = Array (3) {["type"] + = string (4) "View" ["name"] = S  Tring (12) "Company news" ["url"] = = String ("http://www.google.com.tw"} [1] = = Array (3) {["type"] + = String (4) "View" ["name"] + = string (12) "Company case" ["url"] = = string "http://www.baidu.com"} [2] =          > Array (2) {["name"] + = string (12) "Smart home" ["sub_button"] = = Array (5) {[0] = = Array (3) { ["type"] + = string (5) "Click" ["name"] + = string (12) "Function description" ["Key"] + string (Ten) "Smart_ Desc "} [1] = = Array (3) {[" type "] + = string (5)" Click "[" name "] + = string (12)" per "[" "Key"] + string (9) "Day_click"} [2] = = Array (3) {["type"] + = string (5) " Click "[" ["name"] + = string (12) "One point a Day" ["Key"] and "Eve_day_click"} [3] =&G T Array (3) {["type"] + = string (5) "Click" ["name"] + = string (12) "Leisure moment" ["Key"] + string (one) "Xiuxian_day"          } [4] = = Array (3) {["type"] = + string (5) "Click" ["Name"] + string (12) "It's so hard to be a man" ["key"] = = string (3) "Nan"}}}}

  

Find Direct Json_encode ($arr) When adding menu, prompt 40016 error: Number of illegal buttons

After invoking the Json_encode function, all Chinese are converted to theform "\u8fd9",

The following functions (from the Internet) can be used in order to use json_encode-treated structures without changing Chinese:

     /************************************************************** * * Handle all elements in an array with a specific function * @paramstring &$ Array to process the string * @paramstring $function the function to execute * @return Boolean$apply_to_keys_also is also applied to the key * @access public * *********** /function arrayrecursive (& $array, $function, $apply _to_keys    _also = False) {static $recursive _counter = 0;    if (+ + $recursive _counter >) {die (' possible deep recursion attack '); } foreach ($array as $key = + $value) {if (Is_array ($value)) {self::arrayrecursive ($array [$key],        $function, $apply _to_keys_also);        } else {$array [$key] = $function ($value);            } if ($apply _to_keys_also && is_string ($key)) {$new _key = $function ($key);                if ($new _key! = $key) {$array [$new _key] = $array [$key];            Unset ($array [$key]); }}} $recursive _counter--;} /***************** * Convert an array to a JSON string (Chinese language compatible) * @paramarray $array the array to convert * @return The JSON string converted by String * @access public * *************************************************************/function JSON ($ Array) {self::arrayrecursive ($array, ' UrlEncode ', true); $json = Json_encode ($array); return UrlDecode ($json);}

  

Execute add:

function apply () {$token _str = Self::get_access_token (); $del _url = "Https://api.weixin.qq.com/cgi-bin/menu/delete? access_token={$token _str} "; file_get_contents ($del _url); $apply _url =" https://api.weixin.qq.com/cgi-bin/menu/ create?access_token={$token _str} "; $data = Self::json (Self::get_menu ());//json Chinese unchanged $ch = Curl_init (); curl_setopt ($ CH, curlopt_url, $apply _url), curl_setopt ($ch, Curlopt_customrequest, "POST"), curl_setopt ($ch, Curlopt_ssl_ Verifypeer, False); curl_setopt ($ch, Curlopt_ssl_verifyhost, FALSE) curl_setopt ($ch, curlopt_useragent, ' mozilla/5.0 (Compatible; MSIE 5.01; Windows NT 5.0) curl_setopt ($ch, curlopt_followlocation, 1); curl_setopt ($ch, Curlopt_autoreferer, 1); Curl_setopt ($ CH, curlopt_postfields, $data); curl_setopt ($ch, Curlopt_returntransfer, true); $result = Curl_exec ($ch); Curl_close ($ CH); $result = Json_decode ($result, true); $this->redirect ("Menu/index", Array (' msg ' = $result [' Errcode ']==0? ') Operation succeeded ': ' Operation failed '), ' state ' = $result [' Errcode ']==0? 0:1));}

OK add successful ^_^, for reference only

Of course, too much code to the home page is definitely not, but in order to let more people see or thick face to publish to the homepage (I do not know how to stay on the homepage for a few minutes ~

http://www.bkjia.com/PHPjc/440143.html www.bkjia.com true http://www.bkjia.com/PHPjc/440143.html techarticle recently the public platform is very hot, certainly a lot of companies like our company, to develop a public platform, this record in the production of custom menus encountered problems, I hope to give some ...

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