imper86/php-immi-api

immi.shop API 的 PHP SDK

v2.0.0 2023-01-04 17:03 UTC

This package is auto-updated.

Last update: 2024-09-04 20:52:52 UTC


README

安装

只需使用 composer

composer require imper86/php-immi-api

HTTPlug 说明

此库使用 HTTPlug,因此不依赖于任何 HTTP 客户端。为了使用此库,您必须拥有一些 PSR-18 http 客户端PSR-17 http 工厂。如果您不知道应该安装哪些,可以要求这些

composer require php-http/guzzle6-adapter http-interop/http-factory-guzzle

使用方法

库提供了一系列机制,使您无需担心令牌、过期等问题。但是,为了开始使用它,您必须使用 OAuth 流程授权用户。

客户端凭据流程示例

use Http\Client\Common\Plugin\ErrorPlugin;
use Imper86\ImmiApi\Immi;
use Imper86\ImmiApi\Model\Credentials;
use Imper86\ImmiApi\Oauth\FileTokenRepository;
use Imper86\ImmiApi\Plugin\AuthPlugin;

$sandbox = true; //if set to false you will connect to production environment
$credentials = new Credentials(
    'your_client_id',
    'your_client_secret',
    null,
    $sandbox
);
$client = new Immi($credentials);

/*
 * You can invent your own TokenRepository, just implement
 * our TokenRepositoryInterface.
 * You can use your DB, Redis, or anything you want.
 * For this example we use included FileTokenRepository
 */
$tokenRepository = new FileTokenRepository(__DIR__ . '/var/immi_token.json');

if (!$tokenRepository->load()) {
    $tokenRepository->save($client->oauth()->fetchTokenWithClientCredentials());
}

//this plugin will take care of refreshing expired access tokens and will store new ones with $tokenRepository
$client->addPlugin(new AuthPlugin($tokenRepository, $client->oauth()));
//optional, this plugin will throw exceptions on every negative http status code
$client->addPlugin(new ErrorPlugin());

$response = $client->users()->me()->get();

dump(json_decode($response->getBody()->__toString(), true));

授权码流程

如果您想使用它,请与我联系以获取详细信息。

资源

您可以使用授权部分实例化的客户端。

从现在起,您可以在 $client 上使用这些方法

use Imper86\ImmiApi\Immi;
$client->api()->(...)
$client->attributes()->(...)
$client->carts()->(...)
$client->commands()->(...)
$client->contactRequests()->(...)
$client->countries()->(...)
$client->orders()->(...)
$client->products()->(...)
$client->users()->(...)

如果您使用具有类型提示的 IDE(如 PHPStorm),您将很容易找到它。如果没有,请查看 Resource 目录

请注意,并非所有资源都对您可用。许多资源需要管理员角色,因为 API 是在管理员用户和客户端用户之间共享的。

贡献

任何帮助都将非常感激。