tp5小程序生成二维码保存到七牛云

1、先生成二维码保存到服务器

2、上传七牛云删除服务器图片

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\Request;
use app\common\library\Upload;
use Qiniu\Storage\ResumeUploader;  

	public function curl_post_https($url,$data){
    	$curl = curl_init();
    	$headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
	}
  // 图片上传服务器
   	public function UploadImageQrCode($img){

		$fileimgname = time()."-".rand(1000,9999).".png";
	    $filecachs = 'code'.'/'.$fileimgname;
		$fanhuistr = file_put_contents($filecachs,$img);
		
		return $fileimgname;
	}
   /**
     * 获取token
     *
     */
    public function htoken(){
    	$appid = $this->appid;
        $secret = $this->secret;
		$get_token_url =  'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$get_token_url); 
		curl_setopt($ch,CURLOPT_HEADER,0); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); 
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 
		$res = curl_exec($ch); 
		curl_close($ch); 
		$json_obj = json_decode($res,true); 
		return $json_obj['access_token'];
    }
   // 图片上传服务器
    public function erwerima($aid){

        $url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$this->htoken();
        $data=[
          'scene'=>$aid,   
          'path'=>'pages/sofa/sofa',        
          'width'=>130,
          'auto_color'=>false,
        ];
        $data=json_encode($data);
        //拿到二维码
        $result = $this->curl_post_https($url,$data);
        //把二维码存到服务器端
        $res = $this->UploadImageQrCode($result);
        return $res;
    }
    //上传图片至本地、然后上传至七牛云、然后删除本地文件
    public function image($image){
        // 将字符串转换为图片,然后上传七牛云
        $key = 'uploads'.$image;
        $save_path = $image;
       
        $filePath = '.'.$image;
        $accessKey = 'xxxxxxxxx';
        $secretKey = 'xxxxxxxxx';
        $auth = new Auth($accessKey, $secretKey);
        $bucket = "dybd";
        $token = $auth->uploadToken($bucket);
         
        $uploadMgr = new UploadManager();
         
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        
        $url = "七牛云地址".$ret['key'];
      
        //删除本地文件
        $res = unlink($filePath);
        if($res){
            return '/'.$key;
        }else{
            return 1;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值