google / photos-library
PHP Google Photos Library API 客户端库
Requires
- php: ^7.3|^8.0
- ext-bcmath: *
- google/gax: ^1.0.2
- google/protobuf: ^3.7.1
Requires (Dev)
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: ^3.6
README
此存储库包含 Google Photos Library API 的 PHP 客户端库。
您可以在 samples
分支中找到此库的示例。请参阅下面的示例部分。
要求和准备工作
此库依赖于 Composer。如果您还没有在命令行上安装 Composer 作为 composer
,请按照 Linux/unix/OS X 或 Windows 安装指南进行安装。
- 系统要求和依赖项可以在库的
composer.json
文件中找到。如果您不熟悉 Composer,请参阅 此页面 了解更多详细信息。 - 您的 PHP 安装必须包含
bcmath
扩展。 - 如以下所述为您的项目配置 OAuth 2.0 凭据。
下载客户端库
首先,下载库,然后设置 OAuth 2.0 凭据以访问 API。接下来,您可以按照 示例 来查看客户端库的实际应用。
以下是下载此库的一些方法
使用 composer require
按照以下步骤将此库作为第三方库下载到您的项目中。该库将由 Composer 下载并存储在 vendor/
目录下。示例不使用此下载方法下载。
$ composer require google/photos-library
使用 git clone
如果您想修改或为此库做出贡献(例如,提交拉取请求)或希望尝试我们的示例,请使用此方法。当您克隆存储库时,此存储库中的 所有 文件都将被下载。
- 在命令提示符中运行
git clone https://github.com/google/php-photoslibrary.git
。 - 您将获得一个 php-photoslibrary 目录。通过运行
cd php-photoslibrary
来导航到它。 - 在命令提示符中运行
composer install
。这将安装使用库所需的全部依赖项。
下载压缩的 tarball
仅当您想使用此客户端库尝试 Google Photos Library API 时,请使用此方法。tarball 提取目录将仅包含 samples
分支中的示例。
- 在 发布页面 上,选择您想要尝试的版本。然后,在 下载 下,选择您选择的 tarball,例如,php-photoslibrary-samples-vX.Y.Z.tar.gz。
- 将您下载的文件解压到计算机上的任何位置。
- 导航到提取的目录(例如,php-photoslibrary-samples-vX.Y.Z)。
- 在命令提示符中运行
composer install
。这将安装使用库和运行示例所需的全部依赖项。
为PHP设置OAuth2凭据
Google Photos Library API使用OAuth2作为认证机制。请注意,Library API不支持服务帐户。
在以下流程中完成“启用API”和“配置OAuth2.0”步骤,请参考开发者文档中的入门指南
此客户端库与Google Auth Library for PHP一起工作。初始化库时指定客户端密钥JSON文件。在设置PhotosLibraryClient
时使用由认证库返回的认证凭据。请参阅sample/src/common/common.php文件以获取如何操作的示例。
示例用法
学习如何使用此库的最佳方法是审查示例。开发者文档还包括此客户端库在PHP中的代码片段。
设置依赖项和OAuth 2凭据后,您可以访问API。以下是一个简短示例,说明如何创建一个新的相册
// [START sample_usage] use Google\Auth\Credentials\UserRefreshCredentials; use Google\Photos\Library\V1\PhotosLibraryClient; use Google\Photos\Library\V1\PhotosLibraryResourceFactory; try { // Use the OAuth flow provided by the Google API Client Auth library // to authenticate users. See the file /src/common/common.php in the samples for a complete // authentication example. $authCredentials = new UserRefreshCredentials( /* Add your scope, client secret and refresh token here */ ); // Set up the Photos Library Client that interacts with the API $photosLibraryClient = new PhotosLibraryClient(['credentials' => $authCredentials]); // Create a new Album object with at title $newAlbum = PhotosLibraryResourceFactory::album("My Album"); // Make the call to the Library API to create the new album $createdAlbum = $photosLibraryClient->createAlbum($newAlbum); // The creation call returns the ID of the new album $albumId = $createdAlbum->getId(); } catch (\Google\ApiCore\ApiException $exception) { // Error during album creation } catch (\Google\ApiCore\ValidationException $e) { // Error during client creation echo $exception; } // [END sample_usage]
重试配置
默认的重试配置遵循AIP重试API请求的指导,该配置位于photos_library_client_config.json
。
此客户端库使用Google gax-php库进行调用。请参阅其参考文档中的\Google\ApiCore\RetrySettings
,它描述了如何为单个调用、一组API调用或整个客户端实例自定义重试配置。
使用PhotosLibraryClient -> upload (..)
制作的媒体字节上传失败后,除非指定了重试配置,否则不会再次尝试。自定义UploadRetrySettings
以配置重试行为。以下是一个示例,说明如何更改客户端实例的字节上传的重试行为
$uploadRetrySettings = [ 'initialRetryDelayMillis' => 1000, // 1 second 'retryDelayMultiplier' => 1.3, 'maxRetryDelayMillis' => 10000, // 10 seconds 'singleTimeoutMillis' => 900000, // 15 minutes 'maxNumRetries' => 5, 'retryableCodes' => [ApiStatus::DEADLINE_EXCEEDED, ApiStatus::UNAVAILABLE], 'retryableExceptions' => [] ]; $photosLibraryClient = new PhotosLibraryClient([ 'credentials' => $myCredentials, 'uploadRetrySettings' => $uploadRetrySettings ]);
示例
一些示例包含在samples
目录中。它们展示了如何访问媒体项目、过滤媒体、共享相册以及上传文件。
参考文档
此库的PHPDoc可以在本存储库的gh-pages分支中找到。您可以在以下位置在线浏览:https://google.github.io/php-photoslibrary/index.html
有关Google Photos Library API的一般文档,请参阅我们的Google Developers网站:https://developers.google.com/photos
编码风格
我们使用PSR-2作为编码风格标准。假设您在项目的根目录中,要检查编码风格违规,请运行
./vendor/bin/phpcs --standard=phpcs_ruleset.xml -np
要自动修复(可修复的)编码风格违规,请运行
./vendor/bin/phpcbf --standard=phpcs_ruleset.xml
获取支持
对于客户端库特定的错误报告、功能请求和补丁,请在本问题跟踪器上创建问题。
有关其他API问题、错误报告或功能请求,请参阅支持页面。
公告和更新
有关Google Photos Library API和客户端库的更新和新闻,请关注
许可
版权所有2018 Google LLC
本软件受Apache许可证2.0版(以下简称“许可证”)许可;除非按照许可证规定,否则不得使用此文件。您可以在以下地址获取许可证的副本:
https://apache.ac.cn/licenses/LICENSE-2.0
除非适用法律要求或经书面同意,否则在许可证下分发的软件按“现状”分发,不提供任何形式的明示或暗示保证。有关许可证的具体许可和限制条件,请参阅许可证。