为了账号安全,请及时绑定邮箱和手机立即绑定

微信小程序二维码文件流上传到OSS解决方法

标签:
PHP ThinkPHP

前言
        目前项目中有这样一下需求,通过传入的不同的参数生成不同渠道的微信小程序二维码,从而统计各大平台从小程序引流的数据。而旧系统是先通过接口生成二维码后先是保存至当前服务器上后,拿到图片路径和文件信息然后再使用OSS的SDK上传到存储桶。可能是因为生成的二维码是文件流,所以以前的人是通过file_put_contents函数做的保存再使用SDK的文件上传接口。

 

思路
      为了去掉保存至服务器这一步,所以就不能使用文件上传。而目前大平台的存储服务一般都不止提供一种方式的上传,所以找到了文档发现了一个“字符串上传”。也就是当调用微信接口返回的二维码文件流(一堆字符串),然后通过该SDK配置路径和文件就可以实现小程序码的上传了。

 

流程
1. 下载OSS的SDK,可以去其官网也可以composer require aliyuncs/oss-sdk-php

2.  配置参数封装其字符串上传方法等

3.  封装二维码生成方法

4.  实现上传后执行业务操作

编码

1. 文件流上传部分

/*
 *@title 单文件流上传
 *@param string $content 文件流字符串
 *@param string $file_name 存储位置/文件名(包括文件后缀)
 *@return object
*/public function objectUpload($content, $file_name=''){	$config = $this->config;	$res['code'] = 1;	$res['message'] = '';	try{		$ossClient = new OssClient($config['AccessKeyID'], $config['AccessKeySecret'], $config['EndPoint']);		$result = $ossClient->putObject($config['Bucket'], $file_name, $content);		$res['data']['url'] = $result['info']['url'];		$res['data']['path'] = explode("/",$file_name)[1];

	} catch(OssException $e) {		$res['code'] = 0;		$res['message'] = $e->getMessage();
	}	return $res;
}

 

2. 公用文件流上传函数部分

error_reporting(E_ALL ^ E_NOTICE);if (!function_exists('oss_object_upload')) {    function oss_object_upload($content,$file_name)    {        $oss = oss\OssFactory::factory("AliOss");        $result = $oss->objectUpload($content,$file_name);        return $result;
    }
}

 

3. 二维码生成部分

<?php

namespace app\common\library;

use Firebase\JWT\JWT as FirebaseJWT;
use oss\OssFactory;
use think\Db;
use think\Session;
use think\Request;
use think\Loader;
use think\facade\Config;
use GuzzleHttp\Client;


class Wechat{

	protected static $instance = null;
	protected $_error = "";
	protected $_errorMsg = "";
	protected $config;

	public function __construct($type){
        $this->config = Config::pull($type);	// 获取配置参数
    }

    /*
     * @title 获取对象
     * @author beiqiaosu
     * @param string $type (mini:小程序配置)
     * @return null| object
     * */
	public static function getInstance($type='mini')
    {
		if (!self::$instance) {
		    self::$instance = new self($type);
		}

		return self::$instance;
	}

	// 请求微信接口凭证access_token
	public function getAccessToken()
    {
        $res = false;
        if(!$this->config) {
            $this->_error = '请求配置不存在';
            $this->_errorMsg = '接口错误';
        }

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->config['appId']}&secret={$this->config['appSecret']}";

        $client = new Client();
        $response = $client->request('GET', $url, ['verify' => false]);
        $res = $response->getBody()->getContents();
        $result = json_decode(stripcslashes($res), true);

        if (!isset($result['errcode']) || $result['errcode'] == 0) {
            // 获取成功
            $res = $result['access_token'];
        } else {
            $this->_error = $result['errmsg'];
            $this->_errorMsg = '接口错误';
        }
        return $res;
    }

    // 生成小程序自定义二维码
    public function getQrcode($scene)
    {
        $res = false;

        if(empty($scene)) {
            $this->_error = 'scene参数不存在';
            $this->_errorMsg = '接口出错';
        }

        $accrssToken = $this->getAccessToken();
//        if(!$accrssToken) return $res;

        $data = array(
            "scene" => "{$scene}"
        );

        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accrssToken}";

//        $client = new Client(array(
//            'content-type' => 'application/json'
//        ));
//        $paramArr = json_encode($data);
//        $response = $client->post($url, [
//            \GuzzleHttp\RequestOptions::JSON => $data
//        ]);
//        $response = $client->request('POST', $url, ['json'=>$paramArr]);
//        $res = $response->getBody()->getContents();
//        $result = json_decode(stripcslashes($res), true);

        $returnData = self::httpsRequest($url, $data, 'json');

        if (!isset($returnData['errcode']) || $returnData['errcode'] == 0) {
            $res = $returnData;
        }else {
            $this->_error = $returnData['errmsg'];
            $this->_errorMsg = '接口出错';
        }

        return $res;
    }

    // cURL POST请求方法(暂放)
    public static function httpsRequest($url, $data, $type)
    {
        if ($type == 'json') {
            $headers = array("Content-type: application/json;charset=UTF-8", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache");
            $data = json_encode($data);
        }

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $output = curl_exec($curl);

        curl_close($curl);
        return $output;
    }

	public function getError()
    {
		return $this->_error;
	}

	public function getErrorMsg()
    {
		return $this->_errorMsg;
	}

    final protected function __clone()
    {

    }

    public function __call($name, $arguments)
    {
        // TODO: Implement __call() method.
    }

}

 

4. 开始上传的业务实现部分。

// 获取二维码文件流$ret = \app\common\library\Wechat::getInstance()->getQrcode("activty".$activtyId);if(!$ret) {	$res['code'] = 400;	$res['msg'] = \app\common\library\Wechat::getInstance()->getError();	return $res;
}$path = "qrcode/";     // 二维码文件流存放位置$fileName = time() . rand(1000,9999) . ".png";$result = oss_object_upload($ret,$path.$fileName);// 文件流上传失败if(!$result['code']) {	$res['code'] = 400;	$res['msg'] = $result['message'];	return $res;
}// 将二维码图片放入到引流配置中$data = [	"id" => $activtyId,	"img" => $result['data']['url']
];$upRes = Db::name("activity")->update($data);

 

说明

    以上代码是部分,所以需要自己根据业务整理修改合理的采取



点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消