PHP uploads network pictures to Qiniu cloud storage

php---upload network pictures to Qiniu cloud storage

1. Obtain known network image data

Use curl to read image data

public function getImgData($img_url)
    {
        $ch = curl_init($img_url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
        //读取图片数据
        $rawData = curl_exec($ch);
        curl_close($ch);
      
        return $rawData;
    }

2. PHP uploads pictures to Qiniuyun

1. Configure Qiniu information

$accessKey = '七牛云AccessKey';
$secretKey = '七牛云SecretKey';
$qiniu_domain_url = '空间域名';
$bucket = '存储桶名称';

2. Upload pictures, return value: $uploadurl is the URL of pictures uploaded to Qiniu Cloud

         //引入七牛云sdk
        require_once API_ROOT.'/../sdk/qiniu/autoload.php';
        //$img_url:网络图片地址
        $imageData = $this->getImgData($img_url);
        $auth  = new \Qiniu\Auth($accessKey, $secretKey);         
        $token = $auth->uploadToken($bucket);
        $key   = 'qrurl_'.$uid.'.png';//存储图片名称
        $up    = new \Qiniu\Storage\UploadManager();
        $mime  = 'image/jpeg';
        list($rest, $err) = $up->put($token, $key, $imageData, null, $mime);
        if ($err) {
            echo '<pre>';
            print_r($err);
        } else {
            echo '<pre>';
            print_r($rest);
            $uploadurl = $qiniu_domain_url.$rest['key'];//七牛存储图片地址
        }

3. Don't forget to quote the qiniu internal file at the top of the file

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use Qiniu\Storage\BucketManager;

4. The Qiniu SDK download address used in this article:

Link: https://pan.baidu.com/s/1RUuYcKKU7CW1p9Uc_xWx_g 
Extraction code: r5x4 
After copying this content, open the Baidu Netdisk mobile app, which is more convenient to operate

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/u014724048/article/details/114583365