PHP code example of freyo / laravel-entwechat
1. Go to this page and download the library: Download freyo/laravel-entwechat 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/ */
freyo / laravel-entwechat example snippets
Freyo\LaravelEntWechat\ServiceProvider::class,
'LaravelEntWechat' => Freyo\LaravelEntWechat\Facade::class,
WECHAT_APPID
WECHAT_SECRET
WECHAT_TOKEN
WECHAT_AES_KEY
WECHAT_LOG_LEVEL
WECHAT_LOG_FILE
WECHAT_OAUTH_SCOPES
WECHAT_OAUTH_CALLBACK
WECHAT_PAYMENT_MERCHANT_ID
WECHAT_PAYMENT_KEY
WECHAT_PAYMENT_CERT_PATH
WECHAT_PAYMENT_KEY_PATH
WECHAT_PAYMENT_DEVICE_INFO
WECHAT_PAYMENT_SUB_APP_ID
WECHAT_PAYMENT_SUB_MERCHANT_ID
Route::any('/wechat', 'WechatController@serve');
namespace App\Http\Controllers;
use Log;
class WechatController extends Controller
{
/**
* 处理微信的请求消息
*
* @return string
*/
public function serve()
{
Log::info('request arrived.'); # 注意:Log 为 Laravel 组件,所以它记的日志去 Laravel 日志看,而不是 EntWeChat 日志
$wechat = app('wechat');
$wechat->server->setMessageHandler(function($message){
return "欢迎关注 overtrue!";
});
Log::info('return response.');
return $wechat->server->serve();
}
}
namespace App\Http\Controllers;
use EntWeChat\Foundation\Application;
class WechatController extends Controller
{
public function demo(Application $wechat)
{
// $wechat 则为容器中 EntWeChat\Foundation\Application 的实例
}
}
'LaravelEntWechat' => Freyo\LaravelEntWechat\Facade::class,
$wechatServer = LaravelEntWechat::server(); // 服务端
$wechatUser = LaravelEntWechat::user(); // 用户服务
// ... 其它同理
protected $routeMiddleware = [
// ...
'entwechat.oauth' => \Freyo\LaravelEntWechat\Middleware\OAuthAuthenticate::class,
];
//...
Route::group(['middleware' => ['web', 'entwechat.oauth']], function () {
Route::get('/user', function () {
$user = session('wechat.oauth_user'); // 拿到授权用户资料
dd($user);
});
});
Route::group(['middleware' => ['web', 'entwechat.oauth:default']], function () {
// ...
});
shell
php artisan vendor:publish --provider="Freyo\LaravelEntWechat\ServiceProvider"