• 七牛---关于PHP SDK的各种Demo


    文件上传

    上传代码

    <?php
    require_once 'path_to_sdk/vendor/autoload.php';
    
    // 引入鉴权类
    use QiniuAuth;
    
    // 引入上传类
    use QiniuStorageUploadManager;
    
    // 需要填写你的 Access Key 和 Secret Key
    $accessKey = 'Access_Key';
    $secretKey = 'Secret_Key';
    
    // 构建鉴权对象
    $auth = new Auth($accessKey, $secretKey);
    
    // 要上传的空间
    $bucket = 'Bucket_Name';
    
    // 生成上传 Token
    $token = $auth->uploadToken($bucket);
    
    // 要上传文件的本地路径
    $filePath = './php-logo.png';
    
    // 上传到七牛后保存的文件名
    $key = 'my-php-logo.png';
    
    // 初始化 UploadManager 对象并进行文件的上传。
    $uploadMgr = new UploadManager();
    
    // 调用 UploadManager 的 putFile 方法进行文件的上传。
    list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
    echo "
    ====> putFile result: 
    ";
    if ($err !== null) {
       var_dump($err);
    } else {
       var_dump($ret);
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    上传&回调

    <?php
    require_once 'path_to_sdk/vendor/autoload.php';
    
    use QiniuAuth;
    use QiniuStorageUploadManager;
    
    $accessKey = 'Access_Key';
    $secretKey = 'Secret_Key';
    $auth = new Auth($accessKey, $secretKey);
    
    $bucket = 'Bucket_Name';
    // 上传文件到七牛后, 七牛将文件名和文件大小回调给业务服务器.
    
    $policy = array(
        'callbackUrl' => 'http://your.domain.com/callback.php',
        'callbackBody' => 'filename=$(fname)&filesize=$(fsize)'
    );
    $uptoken = $auth->uploadToken($bucket, null, 3600, $policy);
    
    //上传文件的本地路径
    $filePath = './php-logo.png';
    
    $uploadMgr = new UploadManager();
    
    list($ret, $err) = $uploadMgr->putFile($uptoken, null, $filePath);
    echo "
    ====> putFile result: 
    ";
    if ($err !== null) {
        var_dump($err);
    } else {
        var_dump($ret);
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    上传&预转持续化(以视频转码为例)

    <?php
    require_once 'path_to_sdk/vendor/autoload.php';
    
    use QiniuAuth;
    use QiniuStorageUploadManager;
    
    $accessKey = 'Access_Key';
    $secretKey = 'Secret_Key';
    $auth = new Auth($accessKey, $secretKey);
    
    $bucket = 'Bucket_Name';
    
    //转码是使用的队列名称。 
    $pipeline = 'abc';
    
    //要进行转码的转码操作。 
    $fops = "avthumb/mp4/s/640x360/vb/1.25m";
    
    //可以对转码后的文件进行使用saveas参数自定义命名,当然也可以不指定文件会默认命名并保存在当间。
    $savekey = Qiniuase64_urlSafeEncode('目标Bucket_Name:自定义文件key');
    $fops = $fops.'|saveas/'.$savekey;
    
    $policy = array(
      'persistentOps' => $fops,
      'persistentPipeline' => $pipeline
    );
    $uptoken = $auth->uploadToken($bucket, null, 3600, $policy);
    
    //上传文件的本地路径
    $filePath = './php-logo.png';
    
    $uploadMgr = new UploadManager();
    
    list($ret, $err) = $uploadMgr->putFile($uptoken, null, $filePath);
    echo "
    ====> putFile result: 
    ";
    if ($err !== null) {
        var_dump($err);
    } else {
        var_dump($ret);
    }
    
    注:上面的Demo只是针对视频转码,如果需要别的功能比如音视频切片、视频截图、视频拼接只需要修改下上面的fops后面的参数就可以了,
    eg: $fops = "vframe/jpg/offset/1/w/480/h/360/rotate/90"就表示视频截图了。
    下面给出一些常见的数据处理功能,可以根据需要进行选择:
    //------------------图片缩放-------------------
    $fops ='imageView/2/w/200/h/200';
    
    //------------------视频转码-------------------
    // $fops ='avthumb/flv/vb/229k/vcodec/libx264/noDomain/1';
    
    //------------------图片水印-------------------
    $base64URL = Qiniuase64_urlSafeEncode('http://developer.qiniu.com/resource/logo-2.jpg');
    $fops = 'watermark/1/image/'.$base64URL;
    
    //------------------视频切片-------------------
    $fops = 'avthumb/m3u8/pattern/'.$savets;
    //切片与加密参数
    $fops = 'avthumb/m3u8/vb/640k/hlsKey/MDEyMzQ1Njc4OTEyMzQ1Ng==/hlsKeyUrl/aHR0cDovLzd4bGVrYi5jb20yLnowLmdsYi5xaW5pdWNkbi5jb20vcWluaXV0ZXN0LmtleQ==';
    
    //------------------文档转换-------------------
    $fops = 'yifangyun_preview';
    
    //------------------视频截图-------------------
    $fops = 'vframe/jpg/offset/1/w/480/h/360/rotate/90';
    
    //------------------视频拼接-------------------
    //拼接视频片段时要保证所有源的画面长宽值一致
    //除去作为数据处理对象的源文件以外,还可以指定最多5个源文件(即总计6个片段)
    //所有源文件必须属于同一存储空间
    //格式:avconcat/<Mode>/format/<Format>/<encodedUrl0>/<encodedUrl1>/<encodedUrl2>/...
    $encodedUrl1 = Qiniuase64_urlSafeEncode('http://7xl4c9.com1.z0.glb.clouddn.com/pingjie2.flv');
    $encodedUrl2 = Qiniuase64_urlSafeEncode('http://7xl4c9.com1.z0.glb.clouddn.com/pingjie3.avi');
    $fops = 'avconcat/2/format/mp4/'.$encodedUrl1.'/'.$encodedUrl2;
    
    //------------------多文件压缩-------------------
    //可将若干七牛空间中的资源文件,在七牛服务端压缩后存储
    //格式:mkzip/<mode>/url/<Base64EncodedURL>/alias/<Base64EncodedAlias>/url/<Base64EncodedURL>
    $encodedfile1 = Qiniuase64_urlSafeEncode('http://7xl4c9.com1.z0.glb.clouddn.com/photo1.jpg');
    $encodedfile2 = Qiniuase64_urlSafeEncode('http://7xl4c9.com1.z0.glb.clouddn.com/vedio1.mp4');
    $encodedfile3 = Qiniuase64_urlSafeEncode('http://7xl4c9.com1.z0.glb.clouddn.com/audio1.mp3');
    $fops = 'mkzip/2/url/'.$encodedfile1.'/url/'.$encodedfile2.'/url/'.$encodedfile3;
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    可以看到上传成功后的回调行为主要是有上传 Token 中的 put policy 来指定。其中put policy可以指定的行为不止这些,具体可以参考签权类中的policy字段:

    private static $policyFields = array(
        'callbackUrl',
        'callbackBody',
        'callbackHost',
        'callbackBodyType',
        'callbackFetchKey',
    
        'returnUrl',
        'returnBody',
    
        'endUser',
        'saveKey',
        'insertOnly',
    
        'detectMime',
        'mimeLimit',
        'fsizeLimit',
    
        'persistentOps',
        'persistentNotifyUrl',
        'persistentPipeline',
    );
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    下面我们给一个例子来看怎么接收回调信息,并验证该回调是否来自七牛:

    <?php
    require_once 'path_to_sdk/vendor/autoload.php';
    
    use QiniuAuth;
    
    $accessKey = 'Access_Key';
    $secretKey = 'Secret_Key';
    $auth = new Auth($accessKey, $secretKey);
    
    //获取回调的body信息
    $callbackBody = file_get_contents('php://input');
    
    //回调的contentType
    $contentType = 'application/x-www-form-urlencoded';
    
    //回调的签名信息,可以验证该回调是否来自七牛
    $authorization = $_SERVER['HTTP_AUTHORIZATION'];
    
    //七牛回调的url,具体可以参考。
    $url = 'http://your.domain.com/callback.php';
    
    $isQiniuCallback = $auth->verifyCallback($contentType, $authorization, $url, $callbackBody);
    
    if ($isQiniuCallback) {
        $resp = array('ret' => 'success');
    } else {
        $resp = array('ret' => 'failed');
    }
    
    echo json_encode($resp);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    文件下载

    下载代码

    <?php
    require 'path_to_sdk/vendor/autoload.php';
    
    use QiniuAuth;
    
    $accessKey = 'Access_Key';
    $secretKey = 'Secret_Key';
    $auth = new Auth($accessKey, $secretKey);
    
    //baseUrl构造成私有空间的域名/key的形式
    $baseUrl = 'http://domain/key';
    $authUrl = $auth->privateDownloadUrl($baseUrl);
    echo $authUrl;
    ?>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    空间资源的管理

    获取文件的信息

    <?php
    require_once 'path_to_sdk/vendor/autoload.php';
    
    use QiniuAuth;
    use QiniuStorageBucketManager;
    
    $accessKey = 'Access_Key';
    $secretKey = 'Secret_Key';
    
    //初始化Auth状态:
    $auth = new Auth($accessKey, $secretKey);
    
    //初始化BucketManager
    $bucketMgr = new BucketManager($auth);
    
    //你要测试的空间, 并且这个key在你空间中存在
    $bucket = 'Bucket_Name';
    $key = 'php-logo.png';
    
    //获取文件的状态信息
    list($ret, $err) = $bucketMgr->stat($bucket, $key);
    echo "
    ====> $key stat : 
    ";
    if ($err !== null) {
        var_dump($err);
    } else {
        var_dump($ret);
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    移动单个文件

    <?php
    require_once 'path_to_sdk/vendor/autoload.php';
    
    use QiniuAuth;
    use QiniuStorageBucketManager;
    
    $accessKey = 'Access_Key';
    $secretKey = 'Secret_Key';
    
    //初始化Auth状态:
    $auth = new Auth($accessKey, $secretKey);
    
    //初始化BucketManager
    $bucketMgr = new BucketManager($auth);
    
    //你要测试的空间, 并且这个key在你空间中存在
    $bucket = 'Bucket_Name';
    $key = 'php-logo.png';
    
    //获取文件的状态信息
    list($ret, $err) = $bucketMgr->stat($bucket, $key);
    echo "
    ====> $key stat : 
    ";
    if ($err !== null) {
        var_dump($err);
    } else {
        var_dump($ret);
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    复制单个文件

    <?php
    require_once 'path_to_sdk/vendor/autoload.php';
    
    use QiniuAuth;
    use QiniuStorageBucketManager;
    
    $accessKey = 'Access_Key';
    $secretKey = 'Secret_Key';
    
    //初始化Auth状态:
    $auth = new Auth($accessKey, $secretKey);
    
    //初始化BucketManager
    $bucketMgr = new BucketManager($auth);
    
    //你要测试的空间, 并且这个key在你空间中存在
    $bucket = 'Bucket_Name';
    $key = 'php-logo.png';
    
    //将文件从文件$key 复制到文件$key2。 可以在不同bucket复制
    $key2 = 'php-logo2.png';
    $err = $bucketMgr->copy($bucket, $key, $bucket, $key2);
    echo "
    ====> copy $key to $key2 : 
    ";
    if ($err !== null) {
        var_dump($err);
    } else {
        echo "Success!";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    删除单个文件

    <?php
    require_once 'path_to_sdk/vendor/autoload.php';
    
    use QiniuAuth;
    use QiniuStorageBucketManager;
    
    $accessKey = 'Access_Key';
    $secretKey = 'Secret_Key';
    
    //初始化Auth状态:
    $auth = new Auth($accessKey, $secretKey);
    
    //初始化BucketManager
    $bucketMgr = new BucketManager($auth);
    
    //你要测试的空间, 并且这个key在你空间中存在
    $bucket = 'Bucket_Name';
    $key = 'php-logo.png';
    
    //删除$bucket 中的文件 $key
    $err = $bucketMgr->delete($bucket, $key);
    echo "
    ====> delete $key : 
    ";
    if ($err !== null) {
        var_dump($err);
    } else {
        echo "Success!";
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    列举空间中文件

    <?php
    require_once __DIR__ . '/../autoload.php';
    
    use QiniuAuth;
    use QiniuStorageBucketManager;
    
    $accessKey = 'Access_Key';
    $secretKey = 'Secret_Key';
    $auth = new Auth($accessKey, $secretKey);
    $bucketMgr = new BucketManager($auth);
    
    // 要列取的空间名称
    $bucket = 'Bucket_Name';
    
    // 要列取文件的公共前缀
    $prefix = '';
    
    $marker = '';
    $limit = 3;
    
    list($iterms, $marker, $err) = $bucketMgr->listFiles($bucket, $prefix, $marker, $limit);
    if ($err !== null) {
       echo "
    ====> list file err: 
    ";
       var_dump($err);
    } else {
       echo "Marker: $marker
    ";
       echo "
    List Iterms====>
    ";
       var_dump($iterms);
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    触发持久化操作(以视频转码为例)

    视频转码

    <?php
    require_once 'path_to_sdk/vendor/autoload.php';
    
    use QiniuAuth;
    use QiniuProcessingPersistentFop;
    
    //对已经上传到七牛的视频发起异步转码操作 
    
    $accessKey = 'Access_Key';
    $secretKey = 'Secret_Key';
    $auth = new Auth($accessKey, $secretKey);
    
    //要转码的文件所在的空间和文件名。
    $bucket = 'Bucket_Name';
    $key = '1.mp4';
    
    //转码是使用的队列名称。 
    $pipeline = 'abc';
    $pfop = new PersistentFop($auth, $bucket, $pipeline);
    
    //要进行转码的转码操作。   
    $fops = "avthumb/mp4/s/640x360/vb/1.25m";
    
    list($id, $err) = $pfop->execute($key, $fops);
    echo "
    ====> pfop avthumb result: 
    ";
    if ($err != null) {
        var_dump($err);
    } else {
        echo "PersistentFop Id: $id
    ";
    }
    
    //查询转码的进度和状态
    list($ret, $err) = $pfop->status($id);
    echo "
    ====> pfop avthumb status: 
    ";
    if ($err != null) {
        var_dump($err);
    } else {
        var_dump($ret);
    }
    
    
    注:同样,上面的Demo只是针对视频转码,如果需要别的功能比如音视频切片、视频截图、视频拼接只需要修改下上面的fops后面的参数就可以了,
    eg: $fops = "vframe/jpg/offset/1/w/480/h/360/rotate/90"就表示视频截图了。
    下面给出一些常见的数据处理功能,可以根据需要进行选择:
    //------------------图片缩放-------------------
    $fops ='imageView/2/w/200/h/200';
    
    //------------------视频转码-------------------
    // $fops ='avthumb/flv/vb/229k/vcodec/libx264/noDomain/1';
    
    //------------------图片水印-------------------
    $base64URL = Qiniuase64_urlSafeEncode('http://developer.qiniu.com/resource/logo-2.jpg');
    $fops = 'watermark/1/image/'.$base64URL;
    
    //------------------视频切片-------------------
    $fops = 'avthumb/m3u8/pattern/'.$savets;
    //切片与加密参数
    $fops = 'avthumb/m3u8/vb/640k/hlsKey/MDEyMzQ1Njc4OTEyMzQ1Ng==/hlsKeyUrl/aHR0cDovLzd4bGVrYi5jb20yLnowLmdsYi5xaW5pdWNkbi5jb20vcWluaXV0ZXN0LmtleQ==';
    
    //------------------文档转换-------------------
    $fops = 'yifangyun_preview';
    
    //------------------视频截图-------------------
    $fops = 'vframe/jpg/offset/1/w/480/h/360/rotate/90';
    
    //------------------视频拼接-------------------
    //拼接视频片段时要保证所有源的画面长宽值一致
    //除去作为数据处理对象的源文件以外,还可以指定最多5个源文件(即总计6个片段)
    //所有源文件必须属于同一存储空间
    //格式:avconcat/<Mode>/format/<Format>/<encodedUrl0>/<encodedUrl1>/<encodedUrl2>/...
    $encodedUrl1 = Qiniuase64_urlSafeEncode('http://7xl4c9.com1.z0.glb.clouddn.com/pingjie2.flv');
    $encodedUrl2 = Qiniuase64_urlSafeEncode('http://7xl4c9.com1.z0.glb.clouddn.com/pingjie3.avi');
    $fops = 'avconcat/2/format/mp4/'.$encodedUrl1.'/'.$encodedUrl2;
    
    //------------------多文件压缩-------------------
    //可将若干七牛空间中的资源文件,在七牛服务端压缩后存储
    //格式:mkzip/<mode>/url/<Base64EncodedURL>/alias/<Base64EncodedAlias>/url/<Base64EncodedURL>
    $encodedfile1 = Qiniuase64_urlSafeEncode('http://7xl4c9.com1.z0.glb.clouddn.com/photo1.jpg');
    $encodedfile2 = Qiniuase64_urlSafeEncode('http://7xl4c9.com1.z0.glb.clouddn.com/vedio1.mp4');
    $encodedfile3 = Qiniuase64_urlSafeEncode('http://7xl4c9.com1.z0.glb.clouddn.com/audio1.mp3');
    $fops = 'mkzip/2/url/'.$encodedfile1.'/url/'.$encodedfile2.'/url/'.$encodedfile3;
  • 相关阅读:
    程序员面试金典-面试题 16.15. 珠玑妙算
    程序员面试金典-面试题 16.14. 最佳直线
    程序员面试金典-面试题 16.13. 平分正方形
    程序员面试金典-面试题 16.11. 跳水板
    程序员面试金典-面试题 16.10. 生存人数
    程序员面试金典-面试题 16.08. 整数的英语表示
    程序员面试金典-面试题 16.07. 最大数值
    程序员面试金典-面试题 16.06. 最小差
    python学习笔记-47 UDP编程
    python学习笔记-46 TCP编程
  • 原文地址:https://www.cnblogs.com/jjchi/p/9558186.html
Copyright © 2020-2023  润新知