七牛云 SDK for Codeigniter

接连三篇文章都是关于七牛云的,呵呵。感觉我都帮他们做了不小推广了哈。

首发于:https://www.skiy.net/201605094039.html

因为七牛云的 SDK 用了命名空间等高级的很少接触到的货,所以对于我们这种菜逼来说。挺折腾的。
首先,因为没有为 Codeigniter 定制,所以把SDK 放到 libraries 目录还不行,还需要再写几个文件来处理。

制作流程:

1、将 SDK目录php-sdk-7.0.7/src下的 Qiniu 文件夹复制粘贴到 libraries;
2、在 Qiniu 文件夹下新建文件名为Autoloader.php的文件,即Qiniu/Autoloader.php,这一步很关键:
Autoloader.php


 * @link    : https://www.zzzzy.com
 * @created : 5/7/16
 * @modified:
 * @version : 0.0.1
 * @doc     : https://www.zzzzy.com/201605094039.html
 */

namespace Qiniu;

class Autoloader {
    private $directory;
    private $prefix;
    private $prefixLength;

    public function __construct($baseDirectory = __DIR__)
    {
        $this->directory = $baseDirectory;
        $this->prefix = __NAMESPACE__.'\\';
        $this->prefixLength = strlen($this->prefix);
    }

    public function autoload($class)
    {
        if (0 === strpos($class, $this->prefix)) {
            $parts = explode('\\', substr($class, $this->prefixLength));
            $filepath = $this->directory . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $parts) . '.php';

            if (is_file($filepath)) {
                require $filepath;
            }
        }
    }

    public static function register()
    {
        spl_autoload_register(array(new self(), 'autoload'));
    }
}

上面这一段代码几乎照搬了Predis的,这个流弊啊。基本能用了。

3、在 libraries 目录下新建文件为 Qiniu.php,代码如下:

4、使用方式基本和官方相同,只不过因为是后加载的文件,所以无法使用 use 命名空间,所以必须输入全名(new Qiniu\Storage\BucketManager),代码如下:

$this->load->library('Qiniu');

$auth = new Qiniu\Auth($accessKey, $secretKey);
$bucketMgr = new Qiniu\Storage\BucketManager($auth);

你可能感兴趣的:(七牛云 SDK for Codeigniter)