codecamp

七牛云存储接口调用

3.5.1 扩展类库:七牛云存储接口调用

此扩展可用于将图片上传到七牛云存储,或者七牛提供的其他功能。

3.5.2 安装和配置

(1)扩展包下载

 PhalApi-Library 扩展库中下载获取 Qiniu 七牛扩展包,如使用:

git clone https://git.oschina.net/dogstar/PhalApi-Library.git

然后把 Qiniu 目录复制到 ./PhalApi/Library/ 下,即:

cp ./PhalApi-Library/Qiniu/ ./PhalApi/Library/ -R

到此安装完毕!接下是插件的配置。

(2)扩展包配置

我们需要在 ./Config/app.php 配置文件中追加以下配置:

    /**
     * 七牛相关配置
     */
    'Qiniu' =>  array(
        //统一的key
        'accessKey' => '*****',
        'secretKey' => '****',
        //自定义配置的空间
        'space_bucket' => '自定义配置的空间',
        'space_host' => 'http://XXXXX.qiniudn.com',
    ),

你也可以不使用配置文件,直接在初始化注册DI()->qiniu时指定配置。

3.5.3 入门使用

(1)入口注册

//$ vim ./Public/init.php

$loader->addDirs('Library');

// 其他代码....

DI()->qiniu = new Qiniu_Lite();

根据上面所说的,你可以也可以指定配置。

//$ vim ./Public/init.php

$loader->addDirs('Library');

// 其他代码....

$qiniuConfig = array( /* 格式参照配置文件 *);
DI()->qiniu = new Qiniu_Lite($qiniuConfig);

3.5.4 示例:图片上传

先简单写个测试文件:

// vim ./Public/test_qiniu.html 

<html>
    <form method="POST" action="/demo/?service=CDN.uploadFile" enctype="multipart/form-data">
        <input type="file" name="file">
        <input type="submit">
    </form>
</html>

效果如下(通常是提供给客户端上传的):
p

(1)文件上传接口

然后,我们需要编写文件上传接口:

<?php
/**
 * CDN云端接口
 *
 * @author: dogstar 2015-03-17
 */

class Api_CDN extends PhalApi_Api
{
    const CODE_MISS_UPLOAD_FILE = 1;
    const CODE_FAIL_TO_UPLOAD_FILE = 2;
    const CODE_FAIL_TO_UPDATE = 3;

    public function uploadFile()
    {
        $rs = array('code' => self::CODE_FAIL_TO_UPLOAD_FILE, 'url' => '', 'msg' => T('fail to upload file'));

        if (!isset($_FILES['file'])) {
            $rs['code'] = self::CODE_MISS_UPLOAD_FILE;
            $rs['msg'] = T('miss upload file');
            return $rs;
        }

        if ($_FILES["file"]["error"] > 0) {
            $rs['code'] = self::CODE_FAIL_TO_UPLOAD_FILE;
            $rs['msg'] = T('failed to upload file with error: {error}', array('error' => $_FILES['file']['error']));
            DI()->logger->debug('failed to upload file with error: ' . $_FILES['file']['error']);
            return $rs;
        }

        $url = DI()->qiniu->uploadFile($_FILES['file']['tmp_name']);
        if (!empty($url)) {
            $rs['code'] = 0;
            $rs['url'] = $url;
            $rs['msg'] = '';
        }
        @unlink($_FILES['file']['tmp_name']);

        return $rs;
    }
}

(2)运行一下:

缺少上传文件时:

{"ret":200,"data":{"code":1,"url":"","msg":"miss upload file"},"msg":""}

上传失败时:

{"ret":200,"data":{"code":2,"url":"","msg":"fail to upload file"},"msg":""}

可以看到对应的日志(如果不想看到此日志,可通过在初始化日志服务时去掉调试日志):

2015-03-18 00:07:02|DEBUG|failed to upload file to qiniu|{"Err":"bad token","Reqid":"u1EAAG73DB3PVMwT","Details":"","Code":401}

上传成功时:

{"ret":200,"data":{"code":0,"url":"图片链接","msg":""},"msg":""}

优酷开放平台接口调用
新型计划任务
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

八、视频教程

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }