lokalise/php-lokalise-api

Lokalise API 客户端

4.2.0 2024-05-23 08:37 UTC

README

Lokalise API 2.0 的客户端库,用 PHP 编写。完整的 API 参考文档

变更日志位于 此处

入门指南

  1. 需要 PHP 7.4.x 或更高版本
  2. 使用 Composer (推荐) 或手动安装 LokaliseApiClient

Composer 安装

  1. 获取 Composer
  2. 使用以下命令安装 LokaliseApiClient:php composer.phar require lokalise/php-lokalise-api
  3. 将以下内容添加到您应用程序的主要 PHP 文件中:require 'vendor/autoload.php';

构造 LokaliseApiClient

Lokalise 个人资料 创建并获取您的 API 令牌

$client = new \Lokalise\LokaliseApiClient($apiToken);

请求

评论

贡献者

文件

语言

支付卡

项目

队列中的进程

屏幕截图

快照

任务

团队

团队用户

团队用户组

翻译

翻译提供者

订单

Webhooks

分支

响应

/** @var \Lokalise\LokaliseApiResponse $response */
$response = $client->languages->listSystem();

$response->
    headers                // Associative array of Lokalise headers received
    getContent()           // Return response data as associative array
    __toArray()            // getContent() alias. Return response data as associative array
    __toString()           // Return JSON encoded response data
    getTotalCount()        // Return total count of filtered items in List methods
    getPageCount()         // Return count of pages in List methods (based on limit parameter)
    getPaginationLimit()   // Return pagination limit used in the request
    getPaginationPage()    // Return current page of the request
    getNextCursor()        // Return next cursor for cursor based pagination
    hasNextCursor()        // Return true if next cursor is present for cursor based pagination

实用工具

\Lokalise\Utils::
    base64FileEncode($filePath)    // Get base64 encoded contents with leading mime type

异常和错误

\Lokalise\Exceptions\LokaliseApiException       // Exception throws when Lokalise API can't be reached using Guzzle
\Lokalise\Exceptions\LokaliseResponseException  // Exception throws when Lokalise API responded with a single error

最佳实践

$client = new \Lokalise\LokaliseApiClient($apiToken);

try {
    $language = $client->languages->retrieve($projectId, $languageId)->getContent();
} catch (\Lokalise\Exceptions\LokaliseApiException $e) {
    // try again later or break
} catch (\Lokalise\Exceptions\LokaliseResponseException $e) {
    // Request cannot be completed. More details in {$e->getCode()} and {$e->getMessage()}
    // break
}

速率限制

自 2021 年 9 月 14 日起,所有端点的访问限制为每秒 6 个请求。 此限制适用于每个 API 令牌和每个 IP 地址。如果您超过此限制,将返回 429 HTTP 状态代码,并抛出相应的异常。您应妥善处理此类错误。我们建议使用有限重试次数的指数退避机制来处理此类错误。

每个令牌允许并发请求一个。