search
HomeBackend DevelopmentPHP Tutorial微信开发学习日记(8):7步看懂weiphp插件机制,核心目标是响应微信请求

微信开发学习日记(八):7步看懂weiphp插件机制,核心目标是响应微信请求
又经过了几个小时的梳理、回顾,截至目前,终于对weiphp这个框架的机制搞明白了些。想要完全明白,自然还需要大把的时间。

   
第1步:
   配置微信公众号,http://weiphp.jiutianniao.com/ ... .html 

  从上面这个配置可以看出,微信请求呗weiphp的入口文件index.php接收了,可能会被/home/weixin/index/这个action响应。

第2步:
   index.php入口文件,校验了是否是微信请求。 
  /**
 * 微信接入验证
 * 在入口进行验证而不是放到框架里验证,主要是解决验证URL超时的问题
 */
if (! empty ( $_GET ['echostr'] ) && ! empty ( $_GET ["signature"] ) && ! empty ( $_GET ["nonce"] )) {
}

第3步:
  入口已经验证过了,即不是“第一次”  ,请求会被分发到home模块的WeixinController的index这个方法。

第4步:
  在index方法中, 首先获得的就是token、openid、粉丝等信息。
  最关键的处理是,回复数据。
  // 回复数据
$this->reply ( $data, $weixin );

第5步:
   在reply函数中, 就是寻找合适的插件进行消息响应。

   涉及到的问题有,安装了哪些插件、哪些启用了、哪些有权限。
  
    准备了可用插件之后,  根据消息的类型,事件、地理位置、订阅,寻找合适的插件,比如如果有location事件,插件就执行对应的回调方法。

    还有的情况是,消息。

   根据关键字,回复消息。

   这个时候,问题就变成了 插件定位问题。

   a.通过插件标识名、插件名或者自定义关键词来定位处理的插件
   b.通过精准关键词来定位处理的插件 token=0是插件安装时初始化的模糊关键词,所有公众号都可以用
   c.通过模糊关键词来定位处理的插件
   d.最终也无法定位到插件,终止操作 

    疑问:通过代码,发现location等事件,有很多插件都会处理这个事件。而消息,只会找到一个插件。是这么个情况么?

 第6步:
    插件的weixinmodel。

    /**
 * Vote模型
 */
class WeixinAddonModel extends WeixinModel {
function reply($dataArr, $keywordArr = array()) {
}

  微信插件,基本都会有个WeixinModel类,如果有必要,会响应微信的请求。

 第7步:
    URL响应问题。

    这个地方一直有疑惑,如果所谓的插件机制,就是根据用户发送到关键词,让不同的类-插件去响应,这也太没有“含金量” 了吧。难道微信的核心玩法就是这个东东么?

   class VoteController extends AddonsController {
   }

   每个插件,可能会有Controller,响应URL请求。比如,后台管理和前台管理(不会直接和微信进行交互) ,会有一些管理方法,设置信息之类的。

  另外,有些在weixinModel的reply 方法中,可能包含了插件内部定义的url,这个感觉才真的很有价值。

  总结:weiphp这个微信框架,简要总结起来就4点。
1.weiphp基于Thinkphp和Onethink。疑问的是,weiphp有没有对Thinkphp和Onethink的核心代码有改动。如果有改动的话,更多的应该是对Onethink改动。
2.weiphp的核心入口,就是index.php和/home/weixin/index方法,验证和响应微信请求。
3. 插件机制,感觉就是根据关键字找插件,让插件响应请求。
4.后台管理系统、用户端的前端管理、插件机制,都是为了微信框架的核心目标“响应微信请求”。
  这才是最最关键的地方呀。

  武汉九天鸟-武汉领先的移动互联网系统开发商
  九天鸟问答-http://ask.jiutianniao.com
  九天鸟博客-http://blog.jiutianniao.com
  九天鸟官网-建设中,敬请期待
  2015年4月17日 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

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: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

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 and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

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 and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

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.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

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 and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

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.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

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

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

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.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment