The main reason for doing this extension is that there is no mature and maintained WeChat SDK under the yii2 framework so far, and as every back-end person, you may not be able to bypass WeChat development. Therefore, in 2017, PHP Academy opened a course called "Constructing an Open Source WeChat SDK" and promised to open source this extension after the course is completed. It is the predecessor of yii2-wx.
Of course, yii2-wx at this moment has more powerful functions. In addition to supporting the WeChat public account interface, it also supports WeChat applet, open platform (open in subsequent versions), etc.
yii2-wx hosting platform
You can use composer to automatically install and upgrade, Beige also recommends you do this .
<span style="font-size: 14px;">composer require "abei2017/yii2-wx"<br></span>
If there is a problem with composer, please add the -vvv parameter later so that you can see the specific error message.
<span style="font-size: 14px;">composer require "abei2017/yii2-wx" -vvv<br></span>
After successful installation, yii2-wx will be stored in the vendor/abei2017 folder of the program.
It doesn’t matter if composer cannot be used in your environment. You can enter the download page to download the corresponding version and deploy it manually. The steps are as follows :
Extract the installation package and place it in a certain directory of the program (for example, create a new ext folder)
Open the config/web.php file of yii2 and add an alias
<span style="font-size: 14px;">'aliases' => [<br> 'abei2017/wx' => '@app/ext/yii2-wx/src',<br>],<br></span>
Reminder: The alias and component are at the same level, do not put them wrong. The main purpose of using aliases is for normal reference.
Whether it is automatic installation or manual installation, the directory structure of yii2-wx is the same, as follows
<span style="font-size: 14px;">yii2-wx<br>- src<br>-- core //核心文件<br>-- helpers //帮助文件<br>-- mini //小程序接口<br>-- mp //公众号接口<br>-- Application.php //入口文件<br>- composer.json<br>- README.md<br></span>
Follow the above steps and you have successfully installed yii2-wx into your yii2 program. We need to configure it before using it. Down.
<span style="font-size: 14px;">'wx'=>[<br> // 公众号信息<br> 'mp'=>[<br> /**<br> * 账号基本信息,请从微信公众平台<br> */<br> 'app_id' => '', // AppID<br> 'secret' => '', // AppSecret<br> 'token' => '', // Token<br> 'encodingAESKey'=>'',<br> 'safeMode'=>0<br><br> 'payment'=>[<br> 'mch_id' => '',<br> 'key' => '',<br> 'notify_url' => '',<br> 'cert_path' => '', // XXX: 绝对路径!!!!<br> 'key_path' => '', // XXX: 绝对路径!!!!<br> ],<br><br> 'oauth' => [<br> 'scopes' => 'snsapi_userinfo',<br> 'callback' => '',<br> ],<br> ],<br> 'mini'=>[<br> 'app_id' => '', <br> 'secret' => '', <br> 'payment' => [<br> 'mch_id' => '',<br> 'key' => ''<br> ],<br> ]<br>],<br></span>
You only need to ensure that the keys of the arrays in mini and mp remain unchanged. For example, if I want to generate a temporary QR code now, I can implement it with the following code
<span style="font-size: 14px;">$app = new Application(['conf'=>Yii::$app->params['wx']['mp']]);<br>$qrcode = $app->driver("mp.qrcode");<br><br>$result = $qrcode->intTemp(3600,9527);<br></span>
. We always instantiate an Application first and pass the parameters (official account/mini program) , and then use the driver to drive the object of the corresponding interface and use the corresponding method.
The above is the detailed content of yii2-wx installation and upgrade. For more information, please follow other related articles on the PHP Chinese website!