retailcrm / api-client-php
RetailCRM API 的 PHP 客户端
Requires
- php: >=7.3
- ext-json: *
- civicrm/composer-compile-plugin: ^0.20
- doctrine/annotations: ^1.13|^2.0
- liip/serializer: 2.2.* || 2.6.*
- neur0toxine/psr.http-client-implementation.php-http-curl: *
- neur0toxine/psr.http-factory-implementation.nyholm: *
- neur0toxine/psr.http-message-implementation.nyholm: *
- php-http/client-implementation: ^1.0
- php-http/discovery: ^1.13
- php-http/httplug: ^2.2
- psr/cache: ^1.0 || ^2.0 || ^3.0
- psr/event-dispatcher: ^1.0
- psr/http-client: ^1.0
- psr/http-factory: ^1.1
- psr/http-message: ^1.0 || ^2.0
- psr/http-message-implementation: ^1.0
- psr/log: ^1|^2|^3
- symfony/cache: >=v3.1.0
- symfony/console: ^4.0|^5.0|^6.0|^7.0
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: ^0.7.1
- league/container: ^3.3
- league/event: ^3.0
- neur0toxine/pock: ^0.11
- nyholm/psr7: ^1.3
- php-http/curl-client: ^2.2
- phpcompatibility/php-compatibility: ^9.3
- phpmd/phpmd: ^2.10
- phpstan/phpstan: 1.9.14
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.5
- vlucas/phpdotenv: ^5.3
Suggests
- ext-curl: Most HTTP clients need ext-curl to work properly.
- league/event: PSR-14 compatible event dispatcher.
- nyholm/psr7: This is recommended PSR-7 and PSR-17 implementation.
- php-http/client-implementation: PSR-18 compatible client should be available to use this library.
- php-http/curl-client: Simplest PSR-18 client implementation.
- psr/http-factory-implementation: PSR-17 compatible factories should be available to use this library.
- psr/http-message-implementation: PSR-7 compatible HTTP message implementation should be available to use to use this library.
- psr/log-implementation: You can use log implementation for debug purposes.
- symfony/event-dispatcher: PSR-14 compatible event dispatcher.
- symfony/http-client: One of the most popular HTTP clients. Has PSR-18 compatible adapter.
- dev-master / 6.x-dev
- v6.15.7
- v6.15.6
- v6.15.5
- v6.15.4
- v6.15.3
- v6.15.2
- v6.15.1
- v6.15.0
- v6.14.4
- v6.14.3
- v6.14.2
- v6.14.1
- v6.14.0
- v6.13.8
- v6.13.7
- v6.13.6
- v6.13.5
- v6.13.4
- v6.13.3
- v6.13.2
- v6.13.1
- v6.13.0
- 6.12.5
- v6.12.4
- v6.12.3
- 6.12.2
- v6.12.1
- v6.12.0
- v6.11.0
- v6.9.2
- v6.9.1
- v6.9.0
- v6.8.4
- v6.8.3
- v6.8.2
- v6.8.1
- v6.8.0
- v6.7.3
- v6.7.2
- v6.7.1
- v6.7.0
- v6.6.0
- v6.5.0
- v6.4.6
- v6.4.5
- v6.4.4
- v6.4.3
- v6.4.2
- v6.4.1
- v6.4.0
- v6.3.0
- v6.2.2
- v6.2.1
- v6.2.0
- v6.1.6
- v6.1.5
- v6.1.4
- v6.1.3
- v6.1.2
- v6.1.1
- v6.1.0
- v6.0.4
- v6.0.3
- v6.0.2
- v6.0.1
- v6.0.0
- v5.x-dev
- v5.11.4
- v5.11.3
- v5.11.2
- v5.11.1
- v5.10.1
- v5.10.0
- v5.9.0
- v5.8.0
- v5.7.5
- v5.7.4
- v5.7.3
- v5.7.2
- v5.7.1
- v5.7.0
- v5.6.0
- v5.5.4
- v5.5.3
- v5.5.2
- v5.5.1
- v5.5.0
- v5.4.0
- v5.3.0
- v5.2.0
- v5.1.4
- v5.1.3
- v5.1.2
- v5.1.1
- v5.1.0
- v5.0.2
- v5.0.1
- v5.0.0
- v4.x-dev
- v4.1.5
- v4.1.4
- v4.1.3
- v4.1.2
- v4.1.1
- v4.1.0
- 4.0.0
- v3.x-dev
- 3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- 1.3.0
- v1.2.5
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.0
- dev-fixCreateLink
- dev-createLinksOrder
This package is auto-updated.
Last update: 2024-09-10 12:02:26 UTC
README
RetailCRM API PHP 客户端
这是一个 RetailCRM API 的 PHP 客户端。这个库允许使用实际 API 版本。
更多详细信息请参考 文档。
目录
要求
- PHP 7.3 及以上版本
- PHP 的 cURL 支持
- PHP 的 JSON 支持
- 任何兼容 PSR-18 的 HTTP 客户端(安装说明中涵盖)。
- 任何兼容 PSR-17 的 HTTP 工厂实现(安装说明中涵盖)。
- 任何兼容 PSR-7 的 HTTP 消息实现(安装说明中涵盖)。
- 其他在
composer.json
中列出的依赖项(安装说明中涵盖)
安装
按照以下步骤安装库
- 下载并安装 Composer 包管理器。
- 通过执行以下命令从 Packagist 安装库
composer require retailcrm/api-client-php:"~6.0"
在安装过程中,您将看到此消息。在执行时按 'y'
。
civicrm/composer-compile-plugin contains a Composer plugin which is currently not in your allow-plugins config. See https://getcomposer.org.cn/allow-plugins Do you trust "civicrm/composer-compile-plugin" to execute code and wish to enable it now? (writes "allow-plugins" to composer.json) [y,n,d,?]
之后,您可能看到一条类似的消息
The following packages have new compilation tasks:
- retailcrm/api-client-php has 1 task
Allow these packages to compile? ([y]es, [a]lways, [n]o, [l]ist, [h]elp)
通过键入 a
并按 Enter 键选择 [a]lways
。
注意:如果您使用的是 CI/CD 流水线,则应选择 'y'
和 [a]lways
,因为在该环境中不可用交互式终端,这将导致依赖项安装失败。
如果在安装过程中选择了其他选项并且 API 客户端无法正常工作,请按照 这些说明 修复问题。
- 如果没有包含自动加载器,或者您之前没有使用过 Composer,请包含它。
require 'path/to/vendor/autoload.php';
将 path/to/vendor/autoload.php
替换为 Composer 的 autoload.php
的正确路径。
注意:API 客户端使用 php-http/curl-client
和 nyholm/psr7
作为 PSR-18、PSR-17 和 PSR-7 的实现。您可以在安装过程中通过安装您选择的实现来替换这些实现,如下所示
composer require symfony/http-client guzzlehttp/psr7 retailcrm/api-client-php:"~6.0"
更多关于此的信息可以在 文档 中找到。
使用
首先,您应该初始化客户端。最简单的方法是使用 SimpleClientFactory
$client = \RetailCrm\Api\Factory\SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey');
客户端分为几个资源组,所有这些组都可以通过客户端的公共属性访问。您可以通过这些组调用 API 方法,如下所示
$client->api->credentials();
例如,您可以检索客户列表
$client = \RetailCrm\Api\Factory\SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey'); $response = $client->customers->list();
或订单列表
$client = \RetailCrm\Api\Factory\SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey'); $response = $client->orders->list();
要处理错误,您必须使用两种类型的异常
RetailCrm\Api\Interfaces\ClientExceptionInterface
用于网络或其他运行时错误。RetailCrm\Api\Interfaces\ApiExceptionInterface
用于 API 错误。
有关错误处理的示例,请参阅本文件的下一节。
每个资源组负责相应的 API 部分。例如,costs
资源组提供成本操作的方法,而 loyalty
资源组允许与忠诚度计划、账户、奖金等交互。
使用注解来确定您需要哪些 DTO 用于发送请求。如果您的 IDE 没有提供注解,您可能需要配置它们。这将大大简化您使用(以及任何其他)库的工作。
有关用法和示例的更多信息,请参阅 文档。
示例
列出订单
<?php use RetailCrm\Api\Interfaces\ClientExceptionInterface; use RetailCrm\Api\Factory\SimpleClientFactory; use RetailCrm\Api\Interfaces\ApiExceptionInterface; use RetailCrm\Api\Model\Entity\CustomersCorporate\CustomerCorporate; $client = SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey'); try { $response = $client->orders->list(); } catch (ApiExceptionInterface | ClientExceptionInterface $exception) { echo $exception; // Every ApiExceptionInterface and ClientExceptionInterface instance implements __toString() method. exit(-1); } foreach ($response->orders as $order) { printf("Order ID: %d\n", $order->id); printf("First name: %s\n", $order->firstName); printf("Last name: %s\n", $order->lastName); printf("Patronymic: %s\n", $order->patronymic); printf("Phone #1: %s\n", $order->phone); printf("Phone #2: %s\n", $order->additionalPhone); printf("E-Mail: %s\n", $order->email); if ($order->customer instanceof CustomerCorporate) { echo "Customer type: corporate\n"; } else { echo "Customer type: individual\n"; } foreach ($order->items as $item) { echo PHP_EOL; printf("Product name: %s\n", $item->productName); printf("Quantity: %d\n", $item->quantity); printf("Initial price: %f\n", $item->initialPrice); } echo PHP_EOL; printf("Discount: %f\n", $order->discountManualAmount); printf("Total: %f\n", $order->totalSumm); echo PHP_EOL; }
通过 ID 获取特定订单
<?php use RetailCrm\Api\Interfaces\ClientExceptionInterface; use RetailCrm\Api\Interfaces\ApiExceptionInterface; use RetailCrm\Api\Enum\ByIdentifier; use RetailCrm\Api\Factory\SimpleClientFactory; use RetailCrm\Api\Model\Request\BySiteRequest; $client = SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey'); try { $response = $client->orders->get(1234, new BySiteRequest(ByIdentifier::ID, 'site')); } catch (ApiExceptionInterface | ClientExceptionInterface $exception) { echo $exception; // Every ApiExceptionInterface instance should implement __toString() method. exit(-1); } echo 'Order: ' . print_r($response->order, true);
创建新客户
<?php use RetailCrm\Api\Interfaces\ClientExceptionInterface; use RetailCrm\Api\Interfaces\ApiExceptionInterface; use RetailCrm\Api\Factory\SimpleClientFactory; use RetailCrm\Api\Model\Entity\Customers\Customer; use RetailCrm\Api\Model\Request\Customers\CustomersCreateRequest; $client = SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey'); $request = new CustomersCreateRequest(); $request->customer = new Customer(); $request->site = 'aliexpress'; $request->customer->email = 'john.doe@example.com'; $request->customer->firstName = 'John'; $request->customer->lastName = 'Doe'; try { $response = $client->customers->create($request); } catch (ApiExceptionInterface | ClientExceptionInterface $exception) { echo $exception; // Every ApiExceptionInterface instance should implement __toString() method. exit(-1); } echo 'Customer ID: ' . $response->id;
为具有特定电子邮件的用户创建任务
<?php use RetailCrm\Api\Interfaces\ClientExceptionInterface; use RetailCrm\Api\Interfaces\ApiExceptionInterface; use RetailCrm\Api\Factory\SimpleClientFactory; use RetailCrm\Api\Model\Entity\Tasks\Task;use RetailCrm\Api\Model\Filter\Users\ApiUserFilter; use RetailCrm\Api\Model\Request\Tasks\TasksCreateRequest; use RetailCrm\Api\Model\Request\Users\UsersRequest; $client = SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey'); $usersRequest = new UsersRequest(); $usersRequest->filter = new ApiUserFilter(); $usersRequest->filter->email = 'john.doe@example.com'; try { $usersResponse = $client->users->list($usersRequest); } catch (ApiExceptionInterface | ClientExceptionInterface $exception) { echo $exception; // Every ApiExceptionInterface instance should implement __toString() method. exit(-1); } if (0 === count($usersResponse->users)) { echo 'User is not found.'; exit(-1); } $tasksRequest = new TasksCreateRequest(); $tasksRequest->task = new Task(); $tasksRequest->task->performerId = $usersResponse->users[0]->id; $tasksRequest->task->text = 'Do something!'; $tasksRequest->site = 'site'; try { $tasksResponse = $client->tasks->create($tasksRequest); } catch (ApiExceptionInterface | ClientExceptionInterface $exception) { echo $exception; // Every ApiExceptionInterface instance should implement __toString() method. exit(-1); } echo 'Created task with ID: ' . $tasksResponse->id;
上面示例中的错误处理对于实际生产使用已经足够好了。您可以安全地假设 ApiExceptionInterface
是来自 API 的错误,而 ClientExceptionInterface
是客户端错误(例如网络错误或任何运行时错误,使用 HttpClientException
来仅捕获 PSR-18 客户端错误)。然而,如果您愿意,可以实施更复杂的错误处理。
此外,ApiExceptionInterface
和 ClientExceptionInterface
都实现了 __toString()
。这意味着您可以将这些异常转换为字符串,并将结果放入日志中,而无需对异常数据进行特殊处理。
更多示例可以在 文档 中找到。
您可以使用兼容 PSR-14 的事件分配器来接收客户端事件。有关详细信息,请参阅 文档。
注意
此库使用 HTTPlug 抽象。有关更多信息,请访问 官方文档。