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
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

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

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use