mallgroup/mpapi-client

Mall 商场 API 的 PHP 客户端


README

License PHPStan Psalm

商场市场 API 客户端

描述

MPAPI 客户端是一个工具,旨在帮助 Internet Mall, a. s. 合作伙伴使用 Mall 商场 API 容易地管理商品目录、配送、订单等。

要求

  • PHP 7.4PHP 8 的 64 位版本
  • Guzzle 7

安装

要在您的存储库中安装客户端,请使用 Composer 运行以下命令

composer require mallgroup/mpapi-client

实现

信息

客户端由一个主要客户端和多个独立的域名客户端组成。

主要客户端将所有域名客户端分组在一个对象下,以便于实现,但每个域名客户端都可以独立初始化和使用。

每个客户端都提供一个接口,应将其用作代码中的参数类型,而不是客户端类本身(即,使用 MpApiClientInterface $clientBrandsClientInterface $client 而不是 MpApiClient $clientBrandsClient $client)。

初始化客户端时,您必须提供

  1. 实现 AuthMiddlewareInterface 的验证器
    • 目前,只提供了 ClientIdAuthenticator,它接受 my-client-id
    • 将来,将发布新的验证器(例如 OAuth)
  2. 使用 API 的应用程序的名称
    • 它随每个请求发送到 Mall API,以便更容易识别请求和调试报告的问题
    • 请提供一个简单但具有意义的名称,例如 MyAppName CRMMyAppName Order sync,而不是随机字符串

示例

初始化客户端有两种主要方法

1. 使用默认配置的 MpApiClient

<?php declare(strict_types=1);

use MpApiClient\Common\Authenticators\ClientIdAuthenticator;
use MpApiClient\MpApiClient;
use MpApiClient\MpApiClientOptions;

require 'vendor/autoload.php';

// 1. Initialize client options with request authenticator
$options = new MpApiClientOptions(
    new ClientIdAuthenticator('my-client-id')
);

// 2. Initialize MP API Client
$client = MpApiClient::createFromOptions('my-app-name', $options);

// 3. Get brand client
$brandClient = $client->brand();

2. 使用 MpApiClient(或任何其他域名客户端)和自定义 HTTP 客户端

  • 每个域名客户端都可以以此方式初始化为独立的客户端
  • 初始化自定义 HTTP 客户端时,必须提供带有 AuthMiddlewareInterfacehandler 栈!
<?php declare(strict_types=1);

use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use MpApiClient\Brand\BrandClient;
use MpApiClient\Common\Authenticators\ClientIdAuthenticator;
use MpApiClient\MpApiClient;
use MpApiClient\MpApiClientOptions;

require 'vendor/autoload.php';

// 1. Initialize request authenticator
$authenticator = new ClientIdAuthenticator('my-client-id');

// 2. Create custom Guzzle http client with authenticator middleware

// 2.1 Create CurlHandler stack and Guzzle http client manually
$handler      = new CurlHandler();
$handlerStack = HandlerStack::create($handler);
$handlerStack->push($authenticator->getHandler());

$httpClient = new Client(
    [
        'base_uri'        => 'https://mpapi.mallgroup.com/v1/',
        'timeout'         => 10,
        'allow_redirects' => true,
        'handler'         => $handlerStack,
    ]
);

// 2.2. Create Guzzle client using MpApiClientOptions object
$options = new MpApiClientOptions($authenticator);
$options->setTimeout(30);

$httpClient = new Client($options->getGuzzleOptionsArray());

// 3. Create MpApiClient and BrandClient using custom Guzzle client
$client      = new MpApiClient($httpClient, 'my-app-name')
$brandClient = new BrandClient($httpClient, 'my-app-name')

所有客户端域的示例

在此客户端中抛出的自定义异常列表可在此处找到 这里

⚠ 警告

  • 客户端不包括对将来将更改、替换或删除的已弃用端点的支持(例如 /v1/deliveries/v1/gifts

ℹ 已知缺失或不完整的功能

  • /v2/transports 端点的支持