


CI framework WeChat development-custom menu, ci framework custom menu_PHP tutorial
CI framework WeChat development - custom menu, ci framework custom menu
Implemented the custom menu function under the CI framework. Wrote a model and a class library. By the way Comes with access_token implementation
<?<span>php </span><span>class</span><span> Makemenu{ </span><span>public</span> <span>$menustr</span><span>; </span><span>public</span> <span>function</span><span> __construct(){ } </span><span>public</span> <span>function</span><span> init(){ </span><span>$this</span>-><span>dolist(); </span><span>return</span> <span>$this</span>-><span>setmenu(); } </span><span>private</span> <span>function</span><span> dolist(){ </span><span>$CI</span> =&<span> get_instance(); </span><span>$CI</span> -> load ->model("Menu_model","menu"<span>); </span><span>$plist</span> = <span>$CI</span>->menu -><span>isplist(); </span><span>foreach</span>(<span>$plist</span> <span>as</span> <span>$pid</span><span>){ </span><span>$pidarr</span>[] = <span>$pid</span>['pid'<span>]; } </span><span>$list</span> = <span>$CI</span>->menu ->maketree(<span>$CI</span>->menu-><span>getlist()); </span><span>foreach</span>(<span>$list</span> <span>as</span> <span>$btn</span><span>){ </span><span>if</span>(<span>in_array</span>(<span>$btn</span>['id'],<span>$pidarr</span><span>)){ </span><span>//</span><span>生成不带key和url的链接作为父级菜单</span> <span>$btn_arr</span>[<span>$btn</span>['id']] = <span>array</span>("type"=><span>$btn</span>['menutype'], "name"=><span>$btn</span>['content'<span>]); }</span><span>elseif</span>(<span>$btn</span>['pid'] == 0<span>){ </span><span>//</span><span>生成有操作的一级菜单</span> <span>$btn_arr</span>[<span>$btn</span>['id']] = <span>array</span>("type"=><span>$btn</span>['menutype'], "name"=><span>$btn</span>['content'], "key"=><span>$btn</span>['clickkey'], "url"=><span>$btn</span>['url'<span>]); }</span><span>else</span><span>{ </span><span>//</span><span>生成子菜单</span> <span>$btn_arr</span>[<span>$btn</span>['pid']]['sub_button'][] = <span>array</span>("type"=><span>$btn</span>['menutype'], "name"=><span>$btn</span>['content'], "key"=><span>$btn</span>['clickkey'], "url"=><span>$btn</span>['url'<span>]); } } </span><span>$btnarr</span>['button'] = <span>array_values</span>(<span>$btn_arr</span><span>); </span><span>$r</span> = <span>$this</span>->menustr = json_encode(<span>$btnarr</span>,<span>JSON_UNESCAPED_UNICODE); </span><span>return</span> <span>$r</span><span>; } </span><span>private</span> <span>function</span><span> setmenu(){ </span><span>$accesstoken</span> =<span> get_access_token(); </span><span>$url</span> = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={<span>$accesstoken</span>}"<span>; </span><span>$ch</span> =<span> curl_init(); curl_setopt(</span><span>$ch</span>, CURLOPT_URL, <span>$url</span><span>); curl_setopt(</span><span>$ch</span>, CURLOPT_CUSTOMREQUEST, "POST"<span>); curl_setopt(</span><span>$ch</span>, CURLOPT_SSL_VERIFYPEER, <span>FALSE</span><span>); curl_setopt(</span><span>$ch</span>, CURLOPT_SSL_VERIFYHOST, <span>FALSE</span><span>); curl_setopt(</span><span>$ch</span>, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'<span>); curl_setopt(</span><span>$ch</span>, CURLOPT_FOLLOWLOCATION, 1<span>); curl_setopt(</span><span>$ch</span>, CURLOPT_AUTOREFERER, 1<span>); curl_setopt(</span><span>$ch</span>, CURLOPT_POSTFIELDS, <span>$this</span>-><span>menustr); curl_setopt(</span><span>$ch</span>, CURLOPT_RETURNTRANSFER, <span>true</span><span>); </span><span>$info</span> = curl_exec(<span>$ch</span><span>); </span><span>if</span> (curl_errno(<span>$ch</span><span>)) { </span><span>return</span> curl_error(<span>$ch</span><span>); } curl_close(</span><span>$ch</span><span>); </span><span>return</span> <span>$info</span><span>; } }</span>
The above is the content in the library, which mainly generates the json string of the menu based on the data table
<?<span>php </span><span>class</span> Menu_model <span>extends</span><span> CI_Model { </span><span>public</span> <span>$table_name</span><span>; </span><span>public</span> <span>function</span><span> __construct(){ parent</span>::<span>__construct(); </span><span>$this</span>->load-><span>database(); </span><span>$this</span>->table_name = "data_menu"<span>; } </span><span>public</span> <span>function</span> query(<span>$sql</span><span>){ </span><span>return</span> <span>$this</span>->db->query(<span>$sql</span><span>); } </span><span>public</span> <span>function</span> getone(<span>$id</span><span>){ </span><span>$get_sql</span> = "select * from {<span>$this</span>->table_name} where id = {<span>$id</span>}"<span>; </span><span>return</span> <span>$this</span>->query(<span>$get_sql</span>)-><span>row(); } </span><span>public</span> <span>function</span> addone(<span>$data</span><span>){ </span><span>if</span>((<span>$data</span>['pid'] == 0)&&(<span>$this</span>->checksum()>=3<span>)){ </span><span>//</span><span>一级菜单不超过3个</span> <span>return</span> "toomany1"<span>; }</span><span>elseif</span>((<span>$data</span>['pid']!=0)&&(<span>$this</span>->checksum(<span>$data</span>['pid']))>=7<span>){ </span><span>//</span><span>二级菜单不超过7个</span> <span>return</span> "toomany2"<span>; } </span><span>if</span>(<span>is_array</span>(<span>$data</span>)&&!<span>empty</span>(<span>$data</span><span>)){ </span><span>$keys</span> = "`".<span>implode</span>("`,`",<span>array_keys</span>(<span>$data</span>))."`"<span>; </span><span>$vals</span> = "'".<span>implode</span>("','",<span>array_values</span>(<span>$data</span>))."'"<span>; </span><span>$insert_sql</span> = "insert into {<span>$this</span>->table_name} (<span>$keys</span>) values (<span>$vals</span>)"<span>; </span><span>return</span> <span>$this</span>->query(<span>$insert_sql</span><span>); }</span><span>else</span><span>{ </span><span>return</span> <span>false</span><span>; } } </span><span>public</span> <span>function</span> del(<span>$id</span><span>){ </span><span>$infos</span> = <span>$this</span>->getone(<span>$id</span><span>); </span><span>$del_sql</span> = "delete from {<span>$this</span>->table_name} where id = {<span>$id</span>} and pid = {<span>$id</span>}"<span>; </span><span>return</span> <span>$this</span>->query(<span>$del_sql</span><span>); } </span><span>private</span> <span>function</span> checksum(<span>$id</span> = ''<span>){ </span><span>if</span>(<span>$id</span> == ''<span>){ </span><span>$get_sql</span> = "select count(1) as total from {<span>$this</span>->table_name} where pid =0"<span>; }</span><span>else</span><span>{ </span><span>$id</span> = <span>intval</span>(<span>$id</span><span>); </span><span>$get_sql</span> = "select count(1) as total from {<span>$this</span>->table_name} where pid ={<span>$id</span>}"<span>; } </span><span>$r</span> = <span>$this</span>->db->query(<span>$get_sql</span>)-><span>row(); </span><span>return</span> <span>$r</span>-><span>total; } </span><span>public</span> <span>function</span><span> getplist(){ </span><span>//</span><span>获取一级菜单</span> <span>$get_sql</span> = "select * from {<span>$this</span>->table_name} where pid=0 order by menuorder asc"<span>; </span><span>return</span> <span>$this</span>->db->query(<span>$get_sql</span>)-><span>result_array(); } </span><span>public</span> <span>function</span><span> isplist(){ </span><span>$get_sql</span> = "select pid from {<span>$this</span>->table_name} where pid <> 0 group by pid"<span>; </span><span>return</span> <span>$this</span>->db->query(<span>$get_sql</span>)-><span>result_array(); } </span><span>public</span> <span>function</span><span> getlist(){ </span><span>$get_sql</span> = "select * from {<span>$this</span>->table_name} where 1 order by pid asc, menuorder asc"<span>; </span><span>return</span> <span>$this</span>->db->query(<span>$get_sql</span>)-><span>result_array(); } </span><span>public</span> <span>function</span> maketree(<span>$data</span><span>){ </span><span>$pids</span> = <span>array</span><span>(); </span><span>foreach</span>(<span>$data</span> <span>as</span> <span>$k</span>=><span>$v</span><span>){ </span><span>if</span>(<span>$v</span>['pid'] == 0<span>){ </span><span>$pids</span>[<span>$v</span>['id']][] = <span>$v</span><span>; }</span><span>else</span><span>{ </span><span>$pids</span>[<span>$v</span>['pid']][] = <span>$v</span><span>; } } </span><span>list</span>(<span>$t1</span>,<span>$t2</span>,<span>$t3</span>) = <span>array_values</span>(<span>$pids</span><span>); </span><span>$r</span> = <span>array_merge_recursive</span>(<span>is_array</span>(<span>$t1</span>)?<span>$t1</span>:<span>array</span>(),<span>is_array</span>(<span>$t2</span>)?<span>$t2</span>:<span>array</span>(),<span>is_array</span>(<span>$t3</span>)?<span>$t3</span>:<span>array</span><span>()); </span><span>return</span> <span>$r</span><span>; } </span><span>public</span> <span>function</span> update(<span>$data</span><span>){ </span><span>if</span>(<span>is_array</span>(<span>$data</span>)&&!<span>empty</span>(<span>$data</span><span>)){ </span><span>$id</span> = <span>$data</span>['id'<span>]; </span><span>unset</span>(<span>$data</span>['id'<span>]); </span><span>foreach</span>(<span>$data</span> <span>as</span> <span>$k</span>=><span>$v</span><span>){ </span><span>$update_arr</span>[] = "`".<span>$k</span>."` = '".<span>$v</span>."'"<span>; } </span><span>$update_fs</span> = <span>implode</span>(",",<span>$update_arr</span><span>); </span><span>$update_sql</span> = "update {<span>$this</span>->table_name} set {<span>$update_fs</span>} where id = {<span>$id</span>}"<span>; </span><span>return</span> <span>$this</span>->query(<span>$update_sql</span><span>); }</span><span>else</span><span>{ </span><span>return</span> <span>false</span><span>; } } }</span>
The above are various methods in the model.
The table structure of the database is as follows, with statements for creating tables.
<span>CREATE</span> <span>TABLE</span><span> `menu` ( `id` </span><span>int</span>(<span>11</span>) <span>NOT</span> <span>NULL</span><span> AUTO_INCREMENT, `content` </span><span>varchar</span>(<span>20</span>) <span>DEFAULT</span> <span>NULL</span><span>, `pid` </span><span>int</span>(<span>11</span>) <span>DEFAULT</span> <span>'</span><span>0</span><span>'</span><span>, `menutype` enum(</span><span>'</span><span>click</span><span>'</span>,<span>'</span><span>view</span><span>'</span>,<span>'</span><span>scancode_push</span><span>'</span>,<span>'</span><span>scancode_waitmsg</span><span>'</span>,<span>'</span><span>pic_sysphoto</span><span>'</span>,<span>'</span><span>pic_photo_or_album</span><span>'</span>,<span>'</span><span>pic_weixin</span><span>'</span>,<span>'</span><span>location_select</span><span>'</span>) <span>DEFAULT</span> <span>'</span><span>view</span><span>'</span> COMMENT <span>'</span><span>消息类型</span><span>'</span><span>, `url` </span><span>varchar</span>(<span>200</span>) <span>DEFAULT</span> <span>NULL</span> COMMENT <span>'</span><span>链接地址</span><span>'</span><span>, `clickkey` </span><span>varchar</span>(<span>20</span>) <span>DEFAULT</span> <span>NULL</span> COMMENT <span>'</span><span>事件KEY</span><span>'</span><span>, `menuorder` </span><span>int</span>(<span>11</span>) <span>DEFAULT</span> <span>NULL</span> COMMENT <span>'</span><span>排序</span><span>'</span><span>, `submenu` </span><span>tinyint</span>(<span>2</span>) <span>DEFAULT</span> <span>'</span><span>0</span><span>'</span><span>, </span><span>PRIMARY</span> <span>KEY</span><span> (`id`) ) ENGINE</span><span>=</span>MyISAM AUTO_INCREMENT<span>=</span><span>0</span> <span>DEFAULT</span> CHARSET<span>=</span>utf8
Field | Type | Comment | |
主键 | id | int(11) NOT NULL | |
content | varchar(20) NULL | ||
pid | int(11) NULL | ||
menutype | enum('click','view','scancode_push','scancode_waitmsg','pic_sysphoto','pic_photo_or_album','pic_weixin','location_select') NULL | 消息类型 | |
url | varchar(200) NULL | 链接地址 | |
clickkey | varchar(20) NULL | 事件KEY | |
menuorder | int(11) NULL | 排序 | |
submenu | tinyint(2) NULL |
The following is the method of obtaining token written under system/core/common.php. In fact, it needs to be salted, otherwise disgusting people will do disgusting things.
<span>function</span><span> get_access_token(){ </span><span>//</span><span>从微信服务器获取access_token 并保留一个小时</span> <span>$old_filename</span> = APPPATH."cache/".<span>md5</span>(<span>date</span>("YmdH",<span>time</span>()-3600)).".php"<span>; @</span><span>unlink</span>(<span>$old_filename</span><span>); </span><span>$filename</span> = APPPATH."cache/".<span>md5</span>(<span>date</span>("YmdH",<span>time</span>())).".php"<span>; </span><span>if</span>(<span>is_file</span>(<span>$filename</span><span>)){ </span><span>$r</span> = <span>include</span>(<span>$filename</span><span>); }</span><span>else</span><span>{ </span><span>$url</span> = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".<span>APPSECRET; </span><span>$access_token</span> = <span>file_get_contents</span>(<span>$url</span><span>); </span><span>$res</span> = "<?php return ".<span>var_export</span>(json_decode(<span>$access_token</span>,1),1).";"<span>; </span><span>file_put_contents</span>(<span>$filename</span>,<span>$res</span><span>); </span><span>$r</span> = <span>include</span>(<span>$filename</span><span>); } </span><span>return</span> (<span>$r</span>['access_token'<span>]); }</span>
I won’t write about the previous menu management, it’s just to manage the data in that table and make sure that there is no problem with the data in the data table.
Only need
in the controller$this->load->library("Makemenu");
Then call $this->makemenu->dolist();
will be pushed to the WeChat server. You also need to pay attention to defining two constants APPID and APPSECRET in the entry file.
I released it for everyone, I hope it will be useful, and I will make a backup copy for myself.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software