PHP code example of hsinlu / laravel-wechat

1. Go to this page and download the library: Download hsinlu/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/ */

    

hsinlu / laravel-wechat example snippets



json_encode(['group' => [ 'id' => $groupid, 'name' => '我的家人' ],], JSON_UNESCAPED_UNICODE);


'providers' => [
	// ...

	// wechat
	Hsin\Wechat\WechatServiceProvider::class,
],

/**
 * The application's route middleware.
 *
 * @var array
 */
protected $routeMiddleware = [
    // ...

    // wechat
    'wechat.signature' => \Hsin\Wechat\Http\Middleware\CheckWechatSignature::class,
];

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        '/wechat/*'
    ];
}



return [
	// 应用的配置,支持多个应用
	'apps' => [
		// 应用的唯一标识 => 应用配置
		'应用的唯一标识' => [
		    // 应用ID
		    'AppID' => '应用 ID',
		    // 应用密钥
		    'AppSecret' => '应用密钥',
		   	// 令牌
		    'Token' => '令牌',
		    // 消息是否加密 
		    'Encrypt' => false,
		    // 消息加解密密钥
		    'EncodingAESKey' => '消息加解密密钥',
		],
	],
];


wechat()->on('text', function($message) {
	return 'Hello Wechat';
});



namespace App\Wechat\Handlers;

class TextHandler
{
    /**
     * 处理微信发来的文本消息
     *
     * @param  SimpleXMLElement  $message
     * @return void
     */
    public function handle($message)
    {
    	// 处理逻辑
    }
}

wechat()->on('event.subscribe', function($message) {
	return '您已关注。';
});

Hsin\Wechat\Results\TextResult  // 对应文本消息
Hsin\Wechat\Results\ImageResult // 对应图片消息
Hsin\Wechat\Results\VoiceResult // 对应语音消息
Hsin\Wechat\Results\VideoResult // 对应视频消息
Hsin\Wechat\Results\MusicResult // 对应音乐消息
Hsin\Wechat\Results\NewsResult  // 对应图文消息


wechat()->on('text', function ($message) {
     return wechat_result(TextResult::class, [
          'fromUserName' => trim($message->ToUserName),
          'toUserName' => trim($message->FromUserName),
          'content' => '这是一条文本消息。',
     ]);
});

bash
php artisan vendor:publish