PHP code example of meteorlxy / laravel-wechat

1. Go to this page and download the library: Download meteorlxy/laravel-wechat library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

meteorlxy / laravel-wechat example snippets


'providers' => [
    ...
    Meteorlxy\LaravelWechat\WechatServiceProvider::class,
]



namespace App\Http\Controllers\Wechat;

use Illuminate\Http\Request;
use Meteorlxy\LaravelWechat\Controllers\WechatController as BaseController;

class WechatController extends BaseController
{
    public function listen() {
        $this->wechat->server->setHandler(function($message) {
            switch($message->MsgType) {
                case 'event' :
                    $text = '处理文字消息';
                    break;
                case 'event' :
                    $text = '处理事件消息';
                    break;
                default:
                    $text = '默认处理器';
                    break;
            }
            return [
                'Content' => $text,
                
                // 若不设置MsgType,默认回复为Text消息,则必须设置Content
                'MsgType' => 'text',
                
                // 若不设置FromUserName和ToUserName,默认回复给消息来源用户
                'FromUserName' => $message->ToUserName,
                'ToUserName' => $message->FromUserName,
            ];
        });

        return $this->wechat->server->handle($request);
    }
}
        
config/app.php
wechat.php
bash
php artisan vendor:publish
wechat.php