---
title: PHP SDK
---
# PHP SDK 使用指南
此 SDK 适用于 PHP 5.1.0 及其以上版本。基于 [七牛云存储官方API](http://docs.qiniu.com) 构建。使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上。无论您的网络应用是一个网站程序,还是包括从云端(服务端程序)到终端(手持设备应用)的架构的服务或应用,通过七牛云存储及其 SDK,都能让您应用程序的终端用户高速上传和下载,同时也让您的服务端更加轻盈。
SDK源码地址:<https://github.com/qiniu/php-sdk/tags>
- [应用接入](#install)
- [获取Access Key 和 Secret Key](#acc-appkey)
- [资源管理接口](#rs-api)
- [1 查看单个文件属性信息](#rs-stat)
- [2 复制单个文件](#rs-copy)
- [3 移动单个文件](#rs-move)
- [4 删除单个文件](#rs-delete)
- [5 批量操作](#batch)
- [5.1 批量获取文件信息](#batch_stat)
- [5.2 批量复制文件](#batch_copy)
- [5.3 批量删除文件](#batch_delete)
- [5.4 批量移动文件](#batch_move)
- [上传下载接口](#get-and-put-api)
- [1 文件上传](#upload)
- [1.1 上传流程](#io-put-flow)
- [1.2 上传策略](#io-put-policy)
- [1.3 断点续上传](#rio-putfile)
- [2 文件下载](#io-download)
- [2.1 公有资源下载](#public-download)
- [2.2 私有资源下载](#private-download)
- [数据处理接口](#fop-api)
- [1 图像](#fop-image)
- [1.1 查看图像属性](#fop-image-info)
- [1.2 查看图片EXIF信息](#fop-exif)
- [1.3 生成图片预览](#fop-image-view)
- [贡献代码](#contribution)
- [许可证](#license)
<a name=install></a>
## 应用接入
<a name="acc-appkey"></a>
### 1. 获取Access Key 和 Secret Key
要接入七牛云存储,您需要拥有一对有效的 Access Key 和 Secret Key 用来进行签名认证。可以通过如下步骤获得:
1. [开通七牛开发者帐号](https://portal.qiniu.com/signup)
2. [登录七牛开发者自助平台,查看 Access Key 和 Secret Key](https://portal.qiniu.com/setting/key) 。
<a name=rs-api></a>
## 资源管理接口
<a name="rs-stat"></a>
### 1.查看单个文件属性信息
示例代码如下:
```php
require_once('qiniu/rs.php');
$bucket = 'phpsdk';
$key1 = 'file_name_1';
$accessKey = '<YOUR_ACCESS_KEY>';
$secretKey = '<YOUR_SECRET_KEY>';
Qiniu_setKeys($accessKey, $secretKey);
$client = new Qiniu_MacHttpClient(null);
list($ret, $err) = Qiniu_RS_Stat($client, $bucket, $key1);
echo "\n\n====> Qiniu_RS_Stat result: \n";
if ($err !== null) {
var_dump($err);
} else {
var_dump($ret);
}
```
<a name="rs-copy"></a>
### 2. 复制单个文件
示例代码如下:
```php
require_once('qiniu/rs.php');
$bucket = 'phpsdk';
$key1 = 'file_name_1';
$key2 = 'file_name_2';
$accessKey = '<YOUR_ACCESS_KEY>';
$secretKey = '<YOUR_SECRET_KEY>';
Qiniu_setKeys($accessKey, $secretKey);
$client = new Qiniu_MacHttpClient(null);
$err = Qiniu_RS_Copy($client, $bucket, $key1, $bucket, $key2);
echo "\n\n====> Qiniu_RS_Copy result: \n";
if ($err !== null) {
var_dump($err);
} else {
echo "Success! \n";
}
```
<a name=rs-move></a>
### 3. 移动单个文件
示例代码如下:
```php
require_once('qiniu/rs.php');
$bucket = 'phpsdk';
$key1 = 'file_name_1';
$key2 = 'file_name_2';
$accessKey = '<YOUR_ACCESS_KEY>';
$secretKey = '<YOUR_SECRET_KEY>';
Qiniu_setKeys($accessKey, $secretKey);
$client = new Qiniu_MacHttpClient(null);
$err = Qiniu_RS_Move($client, $bucket, $key2, $bucket, $key1);
echo "\n\n====> Qiniu_RS_Move result: \n";
if ($err !== null) {
var_dump($err);
} else {
echo "Success! \n";
}
```
<a name=rs-delete></a>
### 4. 删除单个文件
示例代码如下:
```php
require_once('qiniu/rs.php');
$bucket = 'phpsdk';
$key1 = 'file_name_1';
$accessKey = '<YOUR_ACCESS_KEY>';
$secretKey = '<YOUR_SECRET_KEY>';
Qiniu_setKeys($accessKey, $secretKey);
$client = new Qiniu_MacHttpClient(null);
$err = Qiniu_RS_Delete($client, $bucket, $key1);
echo "\n\n====> Qiniu_RS_Delete result: \n";
if ($err !== null) {
var_dump($err);
} else {
echo "Success! \n";
}
```
<a name=batch></a>
### 5.批量操作
当您需要一次性进行多个操作时,可以使用批量操作。
<a name=batch_stat></a>
#### 5.1 批量获取文件属性信息
示例代码如下:
```php
require_once('qiniu/rs.php');
$bucket = 'phpsdk';
$key1 = 'file_name_1';
$key2 = 'file_name_2';
$accessKey = '<YOUR_ACCESS_KEY>';
$secretKey = '<YOUR_SECRET_KEY>';
Qiniu_setKeys($accessKey, $secretKey);
$e1 = new Qiniu_RS_EntryPath($bucket, $key1);
$e2 = new Qiniu_RS_EntryPath($bucket, $key2);
$client = new Qiniu_MacHttpClient(null);
$entries = array($e1, $e2);
list($ret, $err) = Qiniu_RS_BatchStat($client, $entries);
echo "\n\n====> Qiniu_RS_BatchStat result: \n";
if ($err !== null) {
var_dump($err);
} else {
var_dump($ret);
}
```
<a name=batch_copy></a>
#### 5.2 批量复制文件
示例代码如下:
```php
require_once('qiniu/rs.php');
$bucket = 'phpsdk';
$key1 = 'file_name_1';
$key2 = 'file_name_2';
$accessKey = '<YOUR_ACCESS_KEY>';
$secretKey = '<YOUR_SECRET_KEY>';
Qiniu_setKeys($accessKey, $secretKey);
$e1 = new Qiniu_RS_EntryPath($bucket, $key1);
$e2 = new Qiniu_RS_EntryPath($bucket, $key2);
$key3 = $key1 . '3';
$e3 = new Qiniu_RS_EntryPath($bucket, $key3);
$client = new Qiniu_MacHttpClient(null);
$entryPairs = array(new Qiniu_RS_EntryPathPair($e1, $e2), new Qiniu_RS_EntryPathPair($e1, $e3));
list($ret, $err) = Qiniu_RS_BatchCopy($client, $entryPairs);
echo "\n\n====> Qiniu_RS_BatchCopy result: \n";
if ($err !== null) {
var_dump($err);
} else {
var_dump($ret);
}
```
<a name=batch_delete></a>
#### 5.3 批量删除文件
示例代码如下:
```php
require_once('qiniu/rs.php');
$bucket = 'phpsdk';
$key1 = 'file_name_1';
$key2 = 'file_name_2';
$accessKey = '<YOUR_ACCESS_KEY>';
$secretKey = '<YOUR_SECRET_KEY>';
Qiniu_setKeys($accessKey, $secretKey);
$e1 = new Qiniu_RS_EntryPath($bucket, $key1);
$e2 = new Qiniu_RS_EntryPath($bucket, $key2);
$client = new Qiniu_MacHttpClient(null);
$entries = array($e1, $e2);
list($ret, $err) = Qiniu_RS_BatchDelete($client, $entries);
echo "\n\n====> Qiniu_RS_BatchDelete result: \n";
if ($err !== null) {
var_dump($err);
} else {
var_dump($ret);
}
```
<a name=batch_move></a>
#### 5.4 批量移动文件
示例代码如下:
```php
require_once('qiniu/rs.php');
$bucket = 'phpsdk';
$key1 = 'file_name_1';
$accessKey = '<YOUR_ACCESS_KEY>';
$secretKey = '<YOUR_SECRET_KEY>';
Qiniu_setKeys($accessKey, $secretKey);
$e1 = new Qiniu_RS_EntryPath($bucket, $key1);
$key3 = $key1 . '3';
$e3 = new Qiniu_RS_EntryPath($bucket, $key3);
$client = new Qiniu_MacHttpClient(null);
$entryPairs = array(new Qiniu_RS_EntryPathPair($e3, $e1));
list($ret, $err) = Qiniu_RS_BatchMove($client, $entryPairs);
echo "\n\n====> Qiniu_RS_BatchMove result: \n";
if ($err !== null) {
var_dump($err);
} else {
var_dump($ret);
}
```
<a name="get-and-put-api"></a>
## 上传下载接口
<a name=upload></a>
### 1. 文件上传
为了尽可能地改善终端用户的上传体验,七牛云存储首创了客户端直传功能。一般云存储的上传流程是:
客户端(终端用户) => 业务服务器 => 云存储服务
这样多了一次上传的流程,和本地存储相比,会相对慢一些。但七牛引入了客户端直传,将整个上传过程调整为:
客户端(终端用户) => 七牛 => 业务服务器
客户端(终端用户)直接上传到七牛的服务器,通过DNS智能解析,七牛会选择到离终端用户最近的ISP服务商节点,速度会比本地存储快很多。文件上传成功以后,七牛的服务器使用回调功能,只需要将非常少的数据(比如Key)传给应用服务器,应用服务器进行保存即可。
<a name="io-put-flow"></a>
#### 1.1上传流程
在七牛云存储中,整个上传流程大体分为这样几步:
1. 业务服务器颁发 [uptoken(上传授权凭证)](