PHP code example of jliu / qiniu

1. Go to this page and download the library: Download jliu/qiniu library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jliu / qiniu example snippets




$client = \Qiniu\Qiniu::create(array(
    'access_key' => '<!>',
    'secret_key' => '<!>',
    'bucket'     => '<!>'
));

// 查看文件状态
$res = $client->stat('jobs.jpg');

print_r($res);

if ($res->ok()) {
    // 成功上传或者操作

    // 获取返回的数据
    $data = $res->data; // Or $res->toArray()

    //做一些事情
} else {
    // 失败,获取失败信息
    $res->error;

    // 七牛的Debug头信息
    $res->debug;
}

$res = $client->uploadFile('/home/hfcorriez/Code/jobs.jpg', 'jobs.jpg');

/*
$res->data:

Array
(
    [key] => 4276
    [hash] => FpLJ7yTujtJ85yU6QLA79ZaR3kKg
    [url] => http://php-sdk.qiniudn.com/jobs.jpg
)
*/

$res = $client->upload('I am Qiniu SDK', 'readme.txt');

/*
$res->data:

Array
(
    [key] => 4276
    [hash] => FpLJ7yTujtJ85yU6QLA79ZaR3kKg
    [url] => http://php-sdk.qiniudn.com/readme.txt
)
*/

// 查看文件状态
$res = $client->stat('jobs.jpg');

/*
$res->data:

[data] => Array
(
    [fsize] => 2425435
    [hash] => Fqng6_0hUF8Q-POxmWNi8JD9VRmy
    [mimeType] => image/jpeg
    [putTime] => 13787406554413228
    [url] => http://php-sdk.qiniudn.com/jobs.jpg
)
*/

$res = $client->copy('jobs.jpg', 'jobs.jpg.new');

/*
$res->data:

[data] => Array
(
    [url] => http://php-sdk.qiniudn.com/jobs.jpg.new
)
*/

$res = $client->move('jobs.jpg.new', 'jobs.jpg');

/*
$res->data:

[data] => Array
(
    [url] => http://php-sdk.qiniudn.com/jobs.jpg
)
*/

$res = $client->delete('jobs.jpg');

/*
$res->data:

[data] => Array
(
    [url] => http://php-sdk.qiniudn.com/jobs.jpg
)
*/

$res = $client->ls();

/*
$res->data:

Array
(
    [items] => Array
    (
        [0] => Array
        (
            [fsize] => 2425435
            [putTime] => 13787406554413228
            [key] => jobs.jpg
            [hash] => Fqng6_0hUF8Q-POxmWNi8JD9VRmy
            [mimeType] => image/jpeg
        )
    )
)
*/

$res = $client->batch()
    ->stat('jobs.jpg')
    ->copy('jobs.jpg', 'jobs.jpg.new')
    ->move('jobs.jpg.new', 'jobs.jpg.old')
    ->delete('jobs.jpg.old')
    ->exec();

/*
$res:

Qiniu\Result Object
(
    [error] =>
    [data] => Array
        (
            [0] => Qiniu\Result Object
                (
                    [error] =>
                    [data] => Array
                        (
                            [fsize] => 2425435
                            [hash] => Fqng6_0hUF8Q-POxmWNi8JD9VRmy
                            [mimeType] => image/jpeg
                            [putTime] => 13787430682676023
                            [url] => http://php-sdk.qiniudn.com/jobs.jpg
                        )

                    [response] =>
                    [debug] => Array
                        (
                            [log] => MQ;MC/404;RS.mcd
                            [id] => u00AAFaxXKXYfiIT
                        )

                )

            [1] => Qiniu\Result Object
                (
                    [error] =>
                    [data] => Array
                        (
                            [url] => http://php-sdk.qiniudn.com/jobs.jpg
                        )

                    [response] =>
                    [debug] => Array
                        (
                            [log] => MQ;MC/404;RS.mcd
                            [id] => u00AAFaxXKXYfiIT
                        )

                )

            [2] => Qiniu\Result Object
                (
                    [error] =>
                    [data] => Array
                        (
                            [url] => http://php-sdk.qiniudn.com/jobs.jpg.new
                        )

                    [response] =>
                    [debug] => Array
                        (
                            [log] => MQ;MC/404;RS.mcd
                            [id] => u00AAFaxXKXYfiIT
                        )

                )

            [3] => Qiniu\Result Object
                (
                    [error] =>
                    [data] => Array
                        (
                            [url] => http://php-sdk.qiniudn.com/jobs.jpg.old
                        )

                    [response] =>
                    [debug] => Array
                        (
                            [log] => MQ;MC/404;RS.mcd
                            [id] => u00AAFaxXKXYfiIT
                        )

                )

        )
        ...
 )
*/

$res = $client->imageInfo('qn.jpeg');

/*
$res->data:

Array
(
    [format] => jpeg
    [width] => 640
    [height] => 423
    [colorModel] => ycbcr
)
*/

$res = $client->exif('qn.jpeg');

/*
$res->data:

Array
(
    [ApertureValue] => Array
        (
            [val] => 2.53 EV (f/2.4)
            [type] => 5
        )

    [BrightnessValue] => Array
        (
            [val] => 5.31 EV (135.85 cd/m^2)
            [type] => 10
        )
    ...
)
*/

$url = $client->imageView('jobs.jpg', array("mode" => 1, "width" => 100, "height" => 100, "q" => 50, "format" => 'png'));

/*
$url:

http://php-sdk.qiniudn.com/jobs.jpg?imageView/1/w/100/h/100/q/50/format/png
*/

$res = $client->imageMogr('jobs.jpg', array(
    "thumbnail" => '!50p',
    "gravity"   => 'NorthWest',
    "quality"   => 50,
    "rotate"    => -50,
    "format"    => 'gif'
));

/*
$url:

http://php-sdk.qiniudn.com/jobs.jpg?imageMogr/v2/auto-orient/thumbnail/!50p/gravity/NorthWest/quality/50/rotate/-50/format/gif
*/