PHP code example of suunnn / wechatlib

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

    

suunnn / wechatlib example snippets




return [
    // .env 文件中定义变量
    'miniapp' => [
        'app_id' => env('WECHAT_MINIAPP_ID'),
        'secret' => env('WECHAT_MINIAPP_SECRET'),
        'token' => env('WECHAT_MINIAPP_TOKEN'),
        'aes_key' => env('WECHAT_MINIAPP_AESKEY')
    ],
    'official_account' => [

    ]
];



namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use WeChatLib\MiniApp\AccessToken;
// use WeChatLib\OfficialAccount\AccessToken;
use WeChatLib\MiniApp\Application;
// use WeChatLib\OfficialAccount\Application;

class WeChatMiniAppProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('WeChatMiniApp', function () {
            $app = new Application(config('wechat.miniapp'));   // 实例化一个小程序应用对象
            // $app = new \WeChatLib\OfficialAccount\Application(config('wechat.official_account'));   // 实例化一个公众号应用对象

            //============== 可以设置已有的小程序或公众号 access token ==============/
            // $token = "ACCESS_TOKEN";
            // $accessToken = new AccessToken($app->getAccount()->getAppId(), $app->getAccount()->getSecret());    // 实例化 AccessToken 对象
            // $accessToken->setToken($token); // 传入 access token 的值
            // $app->setAccessToken($accessToken); // 给应用对象传入实例化的 AccessToken 对象
            //=================================================================/

            return $app;
        });
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    public function provides()
    {
        return ['WeChatMiniApp'];
    }
}



namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class WeChatMiniApp extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'WeChatMiniApp';
    }
}


'providers' => [
    /*
     * Package Service Providers...
     */
     App\Providers\WeChatMiniAppProvider::class,
],

'aliases' => [
    ...
    'WeChatMiniApp' => App\Facades\WeChatMiniApp::class,
],



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use WeChatMiniApp;
use Illuminate\Routing\Controller;

class UserController extends Controller
{
    public function store(Request $request)
    {
        // 上传图片
        $response = WeChatMiniApp::getClient()->request(
            'POST',                                         // method 方式
            'https://api.weixin.qq.com/shop/img/upload',    // 微信小程序 api 接口
            // 请求 options 详见 vendor/symfony/http-client-contracts/HttpClientInterface.php
            // 提交微信的数据主要为 body 或 json 等
            [
                'body' => [
                    'resp_type' => 1,
                    'upload_type' => 1,
                    'img_url' => "https://xxx",
                ]
            ]
        )->toArray();

//        // 验证场景值
//        $response = WeChatMiniApp::getClient()->request('POST', 'https://api.weixin.qq.com/shop/scene/check', [
//            'json' => [
//                'scene' => $scene
//            ]
//        ])->toArray();
    }
}



use WeChatLib\MiniApp\AccessToken;
// use WeChatLib\OfficialAccount\AccessToken;
use WeChatLib\MiniApp\Application;
// use WeChatLib\OfficialAccount\Application;

class UserController
{
    public function store()
    {
        $app = new Application(config('wechat.miniapp'));   // 实例化一个小程序应用对象
        // $app = new \WeChatLib\OfficialAccount\Application(config('wechat.official_account'));   // 实例化一个公众号应用对象

        //============== 可以设置已有的小程序或公众号 access token ==============/
        // $token = "ACCESS_TOKEN";
        // $accessToken = new AccessToken($app->getAccount()->getAppId(), $app->getAccount()->getSecret());    // 实例化 AccessToken 对象
        // $accessToken->setToken($token); // 传入 access token 的值
        // $app->setAccessToken($accessToken); // 给应用对象传入实例化的 AccessToken 对象
        //=================================================================/

        $response = $app->getClient()->request(
            'POST',                                         // method 方式
            'https://api.weixin.qq.com/shop/img/upload',    // 微信小程序 api 接口
            // 请求 options 详见 vendor/symfony/http-client-contracts/HttpClientInterface.php
            // 提交微信的数据主要为 body 或 json 等
            [
                'body' => [
                    'resp_type' => 1,
                    'upload_type' => 1,
                    'img_url' => "https://xxx",
                ]
            ]
        )->toArray();

//        // 验证场景值
//        $response = WeChatMiniApp::getClient()->request('POST', 'https://api.weixin.qq.com/shop/scene/check', [
//            'json' => [
//                'scene' => $scene
//            ]
//        ])->toArray();
    }
}



config/wechat.php
app/config.php