PHP code example of caoyuanlianni / laravel-wechat

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

    

caoyuanlianni / laravel-wechat example snippets


  Overtrue\LaravelWechat\ServiceProvider::class,
  

  'EasyWeChat' => Overtrue\LaravelWechat\Facade::class,
  

  $app->register(Overtrue\LaravelWechat\ServiceProvider::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
WECHAT_ENABLE_MOCK

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 日志看,而不是 EasyWeChat 日志

        $wechat = app('wechat');
        $wechat->server->setMessageHandler(function($message){
            return "欢迎关注 overtrue!";
        });

        Log::info('return response.');

        return $wechat->server->serve();
    }
}



namespace App\Http\Controllers;

use EasyWeChat\Foundation\Application;

class WechatController extends Controller
{

    public function demo(Application $wechat)
    {
        // $wechat 则为容器中 EasyWeChat\Foundation\Application 的实例
    }
}

'EasyWeChat' => Overtrue\LaravelWechat\Facade::class,

  $wechatServer = EasyWeChat::server(); // 服务端
  $wechatUser = EasyWeChat::user(); // 用户服务
  // ... 其它同理

protected $routeMiddleware = [
    // ...
    'wechat.oauth' => \Overtrue\LaravelWechat\Middleware\OAuthAuthenticate::class,
];

//...
Route::group(['middleware' => ['web', 'wechat.oauth']], function () {
    Route::get('/user', function () {
        $user = session('wechat.oauth_user'); // 拿到授权用户资料

        dd($user);
    });
});

Route::group(['middleware' => ['web', 'wechat.oauth:snsapi_userinfo']], function () {
  // ...
});

  /*
   * 开发模式下的免授权模拟授权用户资料
   *
   * 当 enable_mock 为 true 则会启用模拟微信授权,用于开发时使用,开发完成请删除或者改为 false 即可
   */
  'enable_mock' => env('WECHAT_ENABLE_MOCK', true),
  'mock_user' => [
     "openid" =>"odh7zsgI75iT8FRh0fGlSojc9PWM",
     // 以下字段为 scope 为 snsapi_userinfo 时需要
     "nickname" => "overtrue",
     "sex" =>"1",
     "province" =>"北京",
     "city" =>"北京",
     "country" =>"中国",
     "headimgurl" => "http://wx.qlogo.cn/mmopen/C2rEUskXQiblFYMUl9O0G05Q6pKibg7V1WpHX6CIQaic824apriabJw4r6EWxziaSt5BATrlbx1GVzwW2qjUCqtYpDvIJLjKgP1ug/0",
],

$event->user; // 同 session('wechat.oauth_user') 一样
$event->isNewSession; // 是不是新的会话(第一次创建 session 时为 true)
shell
  php artisan vendor:publish