# COS-PHP-SDK-V5
腾讯云COS-PHP-SDK-V5([XML API](https://cloud.tencent.com/document/product/436/7751))
[![Total Downloads](https://img.shields.io/packagist/dt/qcloud/cos-sdk-v5.svg?style=flat)](https://packagist.org/packages/qcloud/cos-sdk-v5)
[![Build Status](https://travis-ci.org/tencentyun/cos-php-sdk-v5.svg?branch=master)](https://travis-ci.org/tencentyun/cos-php-sdk-v5)
## 环境准备
* PHP 5.3+
您可以通过`php -v`命令查看当前的PHP版本。
* cURL 扩展
您可以通过`php -m`命令查看cURL扩展是否已经安装好。
> **说明:**
>
> * Ubuntu系统中,您可以使用apt-get包管理器安装PHP的cURL扩展 `sudo apt-get install php-curl`。
> * CentOS系统中,您可以使用yum包管理器安装PHP的cURL扩展 `sudo yum install php-curl`。
### SDK 安装
有三种方式安装SDK:
* Composer方式
* Phar方式
* 源码方式
#### 1、Composer方式
推荐用户使用 Composer 安装 cos-php-sdk-v5,Composer是PHP的依赖管理工具,允许您声明项目所需的依赖,然后自动将它们安装到您的项目中。
> **提示**:您可以在 [getcomposer.org](getcomposer.org) 上找到更多关于如何安装Composer,配置自动加载以及用于定义依赖项的其他最佳实践。
**使用 Composer 安装 COS-PHP-SDK-V5**
1. 打开终端
2. 下载 Composer
```
curl -sS https://getcomposer.org/installer | php
```
3. 创建一个名为`composer.json`的文件,内容为
```
{
"require": {
"qcloud/cos-sdk-v5": "1.*"
}
}
```
4. 使用 Composer 安装
```
php composer.phar install
```
使用该命令后会在当前目录中创建一个vendor文件夹,里面包含 sdk 的依赖库和一个 autoload.php 脚本,方便用户在自己的项目中调用。
5. 通过 autoloader 脚本调用cos-php-sdk-v5
```
require '/path/to/sdk/vendor/autoload.php';
```
现在您的项目已经可以使用COS的V5 SDK了。
#### 2、Phar方式
phar方式安装SDK的步骤如下:
1. 在[github发布页面](https://github.com/tencentyun/cos-php-sdk-v5/releases)下载相应的phar文件
2. 在代码中引入phar文件:
```
require '/path/to/cos-sdk-v5.phar';
```
#### 3、源码方式
源码方式安装SDK的步骤如下:
1. 在[github发布页面](https://github.com/tencentyun/cos-php-sdk-v5/releases)下载相应的zip文件
2. 解压通过 autoload.php 脚本加载sdk
```
require '/path/to/sdk/vendor/autoload.php';
```
## 快速入门
可参照Demo程序,详见 [sample.php](https://github.com/tencentyun/cos-php-sdk-v5/blob/master/sample.php)
## 接口文档
php sdk 接口文档,详见https://cloud.tencent.com/document/product/436/12267
### 配置文件
```php
$cosClient = new Qcloud\Cos\Client(array('region' => 'COS_REGION',
'credentials'=> array(
'secretId' => 'COS_KEY',
'secretKey' => 'COS_SECRET')));
```
### 上传文件
* 使用putObject接口上传文件(最大5G)
* 使用Upload接口分块上传文件
```php
# 上传文件
## putObject(上传接口,最大支持上传5G文件)
### 上传内存中的字符串
//bucket 的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
try {
$result = $cosClient->putObject(array(
'Bucket' => $bucket,
'Key' => $key,
'Body' => 'Hello World!'));
print_r($result);
} catch (\Exception $e) {
echo "$e\n";
}
### 上传文件流
try {
$result = $cosClient->putObject(array(
'Bucket' => $bucket,
'Key' => $key,
'Body' => fopen($local_path, 'rb')));
print_r($result);
} catch (\Exception $e) {
echo "$e\n";
}
### 设置header和meta
try {
$result = $cosClient->putObject(array(
'Bucket' => $bucket,
'Key' => $key,
'Body' => fopen($local_path, 'rb'),
'ACL' => 'string',
'CacheControl' => 'string',
'ContentDisposition' => 'string',
'ContentEncoding' => 'string',
'ContentLanguage' => 'string',
'ContentLength' => integer,
'ContentType' => 'string',
'Expires' => 'mixed type: string (date format)|int (unix timestamp)|\DateTime',
'GrantFullControl' => 'string',
'GrantRead' => 'string',
'GrantWrite' => 'string',
'Metadata' => array(
'string' => 'string',
),
'StorageClass' => 'string'));
print_r($result);
} catch (\Exception $e) {
echo "$e\n";
}
## Upload(高级上传接口,默认使用分块上传最大支持50T)
### 上传内存中的字符串
try {
$result = $cosClient->Upload(
$bucket = $bucket,
$key = $key,
$body = 'Hello World!');
print_r($result);
} catch (\Exception $e) {
echo "$e\n";
}
### 上传文件流
try {
$result = $cosClient->Upload(
$bucket = $bucket,
$key = $key,
$body = fopen($local_path, 'rb'));
print_r($result);
} catch (\Exception $e) {
echo "$e\n";
}
### 设置header和meta
try {
$result = $cosClient->Upload(
$bucket= $bucket,
$key = $key,
$body = fopen($local_path, 'rb'),
$options = array(
'ACL' => 'string',
'CacheControl' => 'string',
'ContentDisposition' => 'string',
'ContentEncoding' => 'string',
'ContentLanguage' => 'string',
'ContentLength' => integer,
'ContentType' => 'string',
'Expires' => 'mixed type: string (date format)|int (unix timestamp)|\DateTime',
'GrantFullControl' => 'string',
'GrantRead' => 'string',
'GrantWrite' => 'string',
'Metadata' => array(
'string' => 'string',
),
'StorageClass' => 'string'));
print_r($result);
} catch (\Exception $e) {
echo "$e\n";
}
```
### 下载文件
* 使用getObject接口下载文件
* 使用getObjectUrl接口获取文件下载URL
```php
# 下载文件
## getObject(下载文件)
### 下载到内存
//bucket 的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
try {
$result = $cosClient->getObject(array(
'Bucket' => $bucket,
'Key' => $key));
echo($result['Body']);
} catch (\Exception $e) {
echo "$e\n";
}
### 下载到本地
try {
$result = $cosClient->getObject(array(
'Bucket' => $bucket,
'Key' => $key,
'SaveAs' => $local_path));
} catch (\Exception $e) {
echo "$e\n";
}
### 指定下载范围
/*
* Range 字段格式为 'bytes=a-b'
*/
try {
$result = $cosClient->getObject(array(
'Bucket' => $bucket,
'Key' => $key,
'Range' => 'bytes=0-10',
'SaveAs' => $local_path));
} catch (\Exception $e) {
echo "$e\n";
}
### 设置返回header
try {
$result = $cosClient->getObject(array(
'Bucket' => $bucket,
'Key' => $key,
'ResponseCacheControl' => 'string',
'ResponseContentDisposition' => 'string',
'ResponseContentEncoding' => 'string',
'ResponseContentLanguage' => 'string',
'ResponseContentType' => 'string',
'ResponseExpires' => 'mixed type: string (date format)|int (unix timestamp)|\DateTime',
'SaveAs' => $local_path));
} catch (\Exception $e) {
echo "$e\n";
}
## getObjectUrl(获取文件UrL)
try {
$signedUrl = $cosClient->getObjectUrl($bucket, $key, '+10 minutes');
echo $signedUrl;
} catch (\Exception $e) {
print_r($e);
}
```
没有合适的资源?快使用搜索试试~ 我知道了~
PHP实例开发源码-WordPress腾讯云COS插件.zip
共579个文件
php:469个
json:37个
rst:30个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 34 浏览量
2022-11-21
20:43:53
上传
评论
收藏 2.75MB ZIP 举报
温馨提示
PHP实例开发源码—WordPress腾讯云COS插件.zip PHP实例开发源码—WordPress腾讯云COS插件.zip PHP实例开发源码—WordPress腾讯云COS插件.zip
资源推荐
资源详情
资源评论
收起资源包目录
PHP实例开发源码-WordPress腾讯云COS插件.zip (579个子文件)
homepage.css 2KB
prettify.css 594B
phpunit.xml.dist 2KB
phpunit.xml.dist 893B
build.properties.dist 391B
.gitignore 368B
.gitignore 34B
index.html 5KB
nav_links.html 316B
leftbar.html 0B
plugins-list.rst.inc 289B
prettify.js 13KB
server.js 5KB
guzzle-schema-1.0.json 9KB
installed.json 5KB
composer.json 2KB
services.json 2KB
composer.json 1KB
test_service.json 1KB
test_service_3.json 1KB
composer.json 1KB
composer.json 815B
composer.json 808B
composer.json 798B
composer.json 766B
composer.json 757B
composer.json 723B
composer.json 713B
composer.json 705B
composer.json 690B
composer.json 679B
composer.json 678B
composer.json 672B
composer.json 669B
composer.json 668B
composer.json 668B
composer.json 664B
composer.json 664B
composer.json 653B
composer.json 626B
composer.json 537B
composer.json 503B
composer.json 471B
json1.json 427B
json2.json 222B
foo.json 131B
test_service2.json 93B
baz.json 45B
recursive.json 33B
bar.json 33B
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
composer.lock 6KB
Makefile 5KB
CHANGELOG.md 47KB
UPGRADING.md 21KB
README.md 7KB
README.md 2KB
CHANGELOG.md 696B
README.md 666B
README.md 610B
mock_response 35B
cacert.pem 245KB
Service.php 208KB
Test.php 46KB
Mimetypes.php 41KB
CurlHandleTest.php 38KB
RequestFactoryTest.php 26KB
Response.php 26KB
RequestTest.php 26KB
Parameter.php 24KB
ResponseTest.php 23KB
ClientTest.php 22KB
XmlVisitorTest.php 21KB
CachePluginTest.php 20KB
sample.php 20KB
Request.php 19KB
CurlMultiTest.php 17KB
CollectionTest.php 17KB
Client.php 16KB
EntityEnclosingRequestTest.php 16KB
Operation.php 15KB
CurlHandle.php 15KB
XmlVisitorTest.php 15KB
CommandTest.php 15KB
Url.php 14KB
CurlMulti.php 14KB
CookieParserProvider.php 14KB
OperationResponseParserTest.php 14KB
AbstractEventDispatcherTest.php 14KB
ParameterTest.php 14KB
ArrayCookieJarTest.php 13KB
ClassLoader.php 13KB
CachePlugin.php 13KB
RequestFactory.php 13KB
OauthPluginTest.php 13KB
Cookie.php 12KB
SchemaValidatorTest.php 12KB
共 579 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
毕业_设计
- 粉丝: 1921
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功