{"id":19585688,"url":"https://github.com/tencentyun/cos-php-sdk-v5","last_synced_at":"2025-05-16T06:04:56.206Z","repository":{"id":39175450,"uuid":"100276500","full_name":"tencentyun/cos-php-sdk-v5","owner":"tencentyun","description":"cos-php-sdk-v5","archived":false,"fork":false,"pushed_at":"2024-05-17T07:45:01.000Z","size":4122,"stargazers_count":182,"open_issues_count":0,"forks_count":81,"subscribers_count":54,"default_branch":"master","last_synced_at":"2024-05-17T08:49:10.091Z","etag":null,"topics":["cos","php-sdk"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tencentyun.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-08-14T14:27:24.000Z","updated_at":"2024-05-28T04:48:12.872Z","dependencies_parsed_at":"2023-11-07T08:27:29.266Z","dependency_job_id":"c52d38f4-3527-4d92-b8bf-f919001c6a38","html_url":"https://github.com/tencentyun/cos-php-sdk-v5","commit_stats":{"total_commits":359,"total_committers":23,"mean_commits":"15.608695652173912","dds":0.3955431754874652,"last_synced_commit":"bb5d04e806fbe038777712e97c94a6ccac885540"},"previous_names":[],"tags_count":67,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tencentyun%2Fcos-php-sdk-v5","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tencentyun%2Fcos-php-sdk-v5/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tencentyun%2Fcos-php-sdk-v5/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tencentyun%2Fcos-php-sdk-v5/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tencentyun","download_url":"https://codeload.github.com/tencentyun/cos-php-sdk-v5/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254478187,"owners_count":22077676,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cos","php-sdk"],"created_at":"2024-11-11T07:56:38.700Z","updated_at":"2025-05-16T06:04:56.184Z","avatar_url":"https://github.com/tencentyun.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# COS-PHP-SDK-V5\n\n腾讯云 COS-PHP-SDK-V5（[XML API](https://cloud.tencent.com/document/product/436/7751)）\n\n[![PHP Version](http://poser.pugx.org/qcloud/cos-sdk-v5/require/php)](https://packagist.org/packages/qcloud/cos-sdk-v5)\n[![License](https://poser.pugx.org/qcloud/cos-sdk-v5/license)](LICENSE)\n[![Latest Stable Version](https://poser.pugx.org/qcloud/cos-sdk-v5/v/stable)](https://packagist.org/packages/qcloud/cos-sdk-v5)\n[![Total Downloads](https://img.shields.io/packagist/dt/qcloud/cos-sdk-v5.svg?style=flat)](https://packagist.org/packages/qcloud/cos-sdk-v5)\n[![Build Status](https://api.travis-ci.com/tencentyun/cos-php-sdk-v5.svg?branch=master)](https://app.travis-ci.com/github/tencentyun/cos-php-sdk-v5)\n[![codecov](https://codecov.io/gh/tencentyun/cos-php-sdk-v5/branch/master/graph/badge.svg)](https://codecov.io/gh/tencentyun/cos-php-sdk-v5)\n[![Support Multiple Versions](https://github.com/tencentyun/cos-php-sdk-v5/actions/workflows/install.yml/badge.svg)](https://github.com/tencentyun/cos-php-sdk-v5/actions/workflows/install.yml)\n\n## 依赖\n\n- [x] PHP \u003e= 5.6\n\u003e 如果您的 php 版本 `\u003e=5.3` 且 `\u003c5.6` , 请使用 [v1.3](https://github.com/tencentyun/cos-php-sdk-v5/tree/1.3) 版本\n\n- [x] ext-curl\n- [x] ext-json\n- [x] ext-simplexml\n- [x] ext-mbstring\n\n## 安装\n\nSDK 安装有三种方式：\n\n- [Composer 方式](#composer-方式)\n- [Phar 方式](#Phar-方式)\n- [源码方式](#源码方式)\n\n### Composer 方式\n\n推荐使用 Composer 安装 cos-php-sdk-v5，Composer 是 PHP 的依赖管理工具，允许您声明项目所需的依赖，然后自动将它们安装到您的项目中。\n\n```bash\ncomposer require qcloud/cos-sdk-v5\n```\n\n\u003e 您可以在 [Composer 官网](https://getcomposer.org/) 上找到更多关于如何安装 Composer，配置自动加载以及用于定义依赖项的其他最佳实践等相关信息。\n\n#### 安装步骤\n\n1. 打开终端；\n2. 下载 Composer，执行以下命令：\n\n```bash\ncurl -sS https://getcomposer.org/installer | php\n```\n\n3. 创建一个名为`composer.json`的文件，内容如下：\n\n```json\n{\n    \"require\": {\n        \"qcloud/cos-sdk-v5\": \"2.*\"\n    }\n}\n```\n\n4. 使用 Composer 安装，执行以下命令：\n\n```bash\nphp composer.phar install\n```\n\n使用该命令后会在当前目录中创建一个 vendor 文件夹，里面包含 SDK 的依赖库和一个 autoload.php 脚本，方便在项目中调用。\n\n5. 通过 autoload.php 脚本调用 cos-php-sdk-v5：\n\n```php\nrequire '/path/to/vendor/autoload.php';\n```\n\n现在您的项目已经可以使用 COS 的 V5 版本 SDK 了。\n\n### Phar 方式\n\nPhar 方式安装 SDK 的步骤如下：\n\n1. 在 [GitHub 发布页面](https://github.com/tencentyun/cos-php-sdk-v5/releases) 下载相应的 phar 文件；\n\u003e 对于 PHP 版本`\u003e= 5.6`且`\u003c7.2.5`的用户请下载`cos-sdk-v5-6.phar`以使用 Guzzle6 版本。  \n\u003e 对于 PHP 版本`\u003e=7.2.5`的用户请下载`cos-sdk-v5-7.phar`以使用 Guzzle7 版本。\n2. 在代码中引入 phar 文件：\n\n```php\nrequire '/path/to/cos-sdk-v5.phar';\n```\n\n### 源码方式\n\n源码方式安装 SDK 的步骤如下：\n\n1. 在 [GitHub 发布页面](https://github.com/tencentyun/cos-php-sdk-v5/releases) 下载相应的 cos-sdk-v5.tar.gz 文件；\n\u003e 对于 PHP 版本`\u003e= 5.6`且`\u003c7.2.5`的用户请下载`cos-sdk-v5-6.tar.gz`以使用 Guzzle6 版本。  \n\u003e 对于 PHP 版本`\u003e=7.2.5`的用户请下载`cos-sdk-v5-7.tar.gz`以使用 Guzzle7 版本。\n2. 解压后通过 autoload.php 脚本加载 SDK：\n\n```php\nrequire '/path/to/cos-sdk-v5/vendor/autoload.php';\n```\n\n## 快速入门\n\n可参照 Demo 程序，详见 [sample 目录](https://github.com/tencentyun/cos-php-sdk-v5/tree/master/sample) 。\n\n## 接口文档\n\nPHP SDK 接口文档，详见 [https://cloud.tencent.com/document/product/436/12267](https://cloud.tencent.com/document/product/436/12267)\n\n### 配置文件\n\n```php\n$cosClient = new Qcloud\\Cos\\Client(array(\n    'region' =\u003e '\u003cRegion\u003e',\n    'credentials' =\u003e array(\n        'secretId' =\u003e '\u003cSecretId\u003e',\n        'secretKey' =\u003e '\u003cSecretKey\u003e'\n    )\n));\n```\n\n若您使用 [临时密钥](https://cloud.tencent.com/document/product/436/14048) 初始化，请用下面方式创建实例。\n\n```php\n$cosClient = new Qcloud\\Cos\\Client(array(\n    'region' =\u003e '\u003cRegion\u003e',\n    'credentials' =\u003e array(\n        'secretId' =\u003e '\u003cSecretId\u003e',\n        'secretKey' =\u003e '\u003cSecretKey\u003e',\n        'token' =\u003e '\u003cXCosSecurityToken\u003e'\n    )\n));\n```\n\n### 上传文件\n\n- 使用 putObject 接口上传文件(最大 5G)\n- 使用 Upload 接口分块上传文件\n\n```php\n# 上传文件\n## putObject(上传接口，最大支持上传5G文件)\n### 上传内存中的字符串\n//bucket 的命名规则为{name}-{appid} ，此处填写的存储桶名称必须为此格式\ntry {\n    $result = $cosClient-\u003eputObject(array(\n        'Bucket' =\u003e $bucket,\n        'Key' =\u003e $key,\n        'Body' =\u003e 'Hello World!'));\n    print_r($result);\n} catch (\\Exception $e) {\n    echo \"$e\\n\";\n}\n\n### 上传文件流\ntry {\n    $result = $cosClient-\u003eputObject(array(\n        'Bucket' =\u003e $bucket,\n        'Key' =\u003e $key,\n        'Body' =\u003e fopen($local_path, 'rb')));\n    print_r($result);\n} catch (\\Exception $e) {\n    echo \"$e\\n\";\n}\n\n### 设置header和meta\ntry {\n    $result = $cosClient-\u003eputObject(array(\n        'Bucket' =\u003e $bucket,\n        'Key' =\u003e $key,\n        'Body' =\u003e fopen($local_path, 'rb'),\n        'ACL' =\u003e 'string',\n        'CacheControl' =\u003e 'string',\n        'ContentDisposition' =\u003e 'string',\n        'ContentEncoding' =\u003e 'string',\n        'ContentLanguage' =\u003e 'string',\n        'ContentLength' =\u003e integer,\n        'ContentType' =\u003e 'string',\n        'Expires' =\u003e 'mixed type: string (date format)|int (unix timestamp)|\\DateTime',\n        'Metadata' =\u003e array(\n            'string' =\u003e 'string',\n        ),\n        'StorageClass' =\u003e 'string'));\n    print_r($result);\n} catch (\\Exception $e) {\n    echo \"$e\\n\";\n}\n\n## Upload(高级上传接口，默认使用分块上传最大支持50T)\n### 上传内存中的字符串\ntry {\n    $result = $cosClient-\u003eUpload(\n        $bucket = $bucket,\n        $key = $key,\n        $body = 'Hello World!');\n    print_r($result);\n} catch (\\Exception $e) {\n    echo \"$e\\n\";\n}\n\n### 上传文件流\ntry {\n    $result = $cosClient-\u003eUpload(\n        $bucket = $bucket,\n        $key = $key,\n        $body = fopen($local_path, 'rb'));\n    print_r($result);\n} catch (\\Exception $e) {\n    echo \"$e\\n\";\n}\n\n### 设置header和meta\ntry {\n    $result = $cosClient-\u003eUpload(\n        $bucket= $bucket,\n        $key = $key,\n        $body = fopen($local_path, 'rb'),\n        $options = array(\n            'ACL' =\u003e 'string',\n            'CacheControl' =\u003e 'string',\n            'ContentDisposition' =\u003e 'string',\n            'ContentEncoding' =\u003e 'string',\n            'ContentLanguage' =\u003e 'string',\n            'ContentLength' =\u003e integer,\n            'ContentType' =\u003e 'string',\n            'Expires' =\u003e 'mixed type: string (date format)|int (unix timestamp)|\\DateTime',\n            'Metadata' =\u003e array(\n                'string' =\u003e 'string',\n            ),\n            'StorageClass' =\u003e 'string'));\n    print_r($result);\n} catch (\\Exception $e) {\n    echo \"$e\\n\";\n}\n```\n\n### 下载文件\n\n- 使用 getObject 接口下载文件\n- 使用 getObjectUrl 接口获取文件下载 URL\n\n```php\n# 下载文件\n## getObject(下载文件)\n### 下载到内存\n//bucket 的命名规则为{name}-{appid} ，此处填写的存储桶名称必须为此格式\ntry {\n    $result = $cosClient-\u003egetObject(array(\n        'Bucket' =\u003e $bucket,\n        'Key' =\u003e $key));\n    echo($result['Body']);\n} catch (\\Exception $e) {\n    echo \"$e\\n\";\n}\n\n### 下载到本地\ntry {\n    $result = $cosClient-\u003egetObject(array(\n        'Bucket' =\u003e $bucket,\n        'Key' =\u003e $key,\n        'SaveAs' =\u003e $local_path));\n} catch (\\Exception $e) {\n    echo \"$e\\n\";\n}\n\n### 指定下载范围\n/*\n * Range 字段格式为 'bytes=a-b'\n */\ntry {\n    $result = $cosClient-\u003egetObject(array(\n        'Bucket' =\u003e $bucket,\n        'Key' =\u003e $key,\n        'Range' =\u003e 'bytes=0-10',\n        'SaveAs' =\u003e $local_path));\n} catch (\\Exception $e) {\n    echo \"$e\\n\";\n}\n\n### 设置返回header\ntry {\n    $result = $cosClient-\u003egetObject(array(\n        'Bucket' =\u003e $bucket,\n        'Key' =\u003e $key,\n        'ResponseCacheControl' =\u003e 'string',\n        'ResponseContentDisposition' =\u003e 'string',\n        'ResponseContentEncoding' =\u003e 'string',\n        'ResponseContentLanguage' =\u003e 'string',\n        'ResponseContentType' =\u003e 'string',\n        'ResponseExpires' =\u003e 'mixed type: string (date format)|int (unix timestamp)|\\DateTime',\n        'SaveAs' =\u003e $local_path));\n} catch (\\Exception $e) {\n    echo \"$e\\n\";\n}\n\n## getObjectUrl(获取文件Url)\ntry {\n    $signedUrl = $cosClient-\u003egetObjectUrl($bucket, $key, '+10 minutes');\n    echo $signedUrl;\n} catch (\\Exception $e) {\n    echo \"$e\\n\";\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftencentyun%2Fcos-php-sdk-v5","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftencentyun%2Fcos-php-sdk-v5","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftencentyun%2Fcos-php-sdk-v5/lists"}