laravel 获取小程序二维码

EasyWechat 小程序获取二维码

1.
	composer require overtrue/wechat:~4.0 -vvv
2.
	<?php
	namespace App\Services;
	use EasyWeChat\Factory;
	use Illuminate\Support\Facades\Storage;
	class WechatService
	{
	    public function index($path,$width)
	    {
	        $config = [
	            'app_id' => env('DEFAULT_APPID'),
	            'secret' => env('APPSECRET'),
	            // 下面为可选项
	            // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
	            'response_type' => 'array',
	
	            'log' => [
	                'level' => 'debug',
	                'file' => __DIR__.'/wechat.log',
	            ],
	        ];
	        $app = Factory::miniProgram($config);
	        $response = $app->app_code->getQrCode($path,$width);
	        // 保存小程序码到文件
	        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
	            $contents = $response->getBody()->getContents();
	            $qrcode_url = '/' . env('FILE_DIR') . '/qrcode/' . date('Ymd') . '/' . str_random(32) . uniqid() . '.jpg';
	
	            if (Storage::put($qrcode_url, $contents)) {
	                return env('FILE_CUSTOM_DOMAIN') . $qrcode_url;
	            }
	            return false;
	        }
	        return false;
	    }
	}
发布了21 篇原创文章 · 获赞 7 · 访问量 1411

猜你喜欢

转载自blog.csdn.net/weixin_37647596/article/details/103576782