ixolit/dislo-sdk

此软件包已被废弃且不再维护。作者建议使用 ixoplan/ixoplan-sdk 软件包。

Ixoplan API 的客户端库

1.0.52 2022-01-11 14:02 UTC

README

Ixoplan API 的 PHP SDK

安装

只需将 ixolit/ixoplan-sdkixolit/ixoplan-sdk-http 的提供者(例如 ixolit/ixoplan-sdk-http-guzzle)添加到您的 composer.json 中,例如:

{
    "name": "myvendor/myproject",
    "description": "Using ixoplan-sdk",
    "require": {
        "ixolit/ixoplan-sdk": "*",
        "ixolit/ixoplan-sdk-http-guzzle": "*"
    }
}

使用方法

实例化客户端

客户端设计用于不同的传输层。它需要一个 RequestClient 接口(例如 HTTPRequestClient)来实际与 Ixoplan 通信。

use Ixolit\Dislo\Client;
use Ixolit\Dislo\HTTP\Guzzle\GuzzleHTTPClientAdapter;
use Ixolit\Dislo\Request\HTTPRequestClient;
    
$httpAdapter = new GuzzleHTTPClientAdapter();

$httpClient = new HTTPRequestClient(
    $httpAdapter,
    $host,
    $apiKey,
    $apiSecret
);

$apiClient = new Client($httpClient);

与用户数据相关的多数方法可以使用用户 ID、\Ixolit\Dislo\WorkingObjects\User 对象或认证令牌。但是,不安全的选项必须通过将 $forceTokenMode = false 传递给构造函数来明确请求。除非您真的需要它,例如实现管理功能,否则请勿使用此选项。

登录

认证用户,获取令牌和用户数据

$apiClient = new \Ixolit\Dislo\Client($httpClient);

try {
    $authResponse = $apiClient->userAuthenticate(
        $userEmail,
        $password,
        $ipAddress
    );

    $authToken = $authResponse->getAuthToken();
    setcookie('authToken', $authToken);
        
    $user = $response->getUser();
    echo $user->getLastLoginDate();
}
catch (\Ixolit\Dislo\Exceptions\AuthenticationInvalidCredentialsException $e) {
    // invalid credentials
    echo $e->getMessage();
}

从令牌获取用户

$token = getcookie('authToken');

$apiClient = new \Ixolit\Dislo\Client($httpClient);
    
try {
    $user = $apiClient->userGet($token);
}
catch (\Ixolit\Dislo\Exceptions\InvalidTokenException $e) {
    // token invalid, e.g. expired
    setcookie('authToken', null, -1);
}
catch (\Ixolit\Dislo\Exceptions\DisloException $e) {
    // other, e.g. missing arguments
}

令牌的过期时间在使用时会自动延长。

验证令牌,显式延长其过期时间,并可选地更改其有效期

$token = getcookie('authToken');

$apiClient = new \Ixolit\Dislo\Client($httpClient);
    
try {
    $response = $apiClient->userExtendToken($token, $ipAdress, 3600);
    $authToken = $response->getAuthToken();
    echo $authToken->getValidUntil()->format('c');
}
catch (\Ixolit\Dislo\Exceptions\InvalidTokenException $e) {
    // token invalid, e.g. expired
    setcookie('authToken', null, -1);
}
catch (\Ixolit\Dislo\Exceptions\DisloException $e) {
    // other, e.g. missing arguments
}

注销

$apiClient->userDeauthenticate($token);
setcookie('authToken', null, -1);

软件包

检索软件包列表,可选地通过服务 ID 过滤

$apiClient = new \Ixolit\Dislo\Client($httpClient);

$response = $apiClient->packagesList("1");

foreach ($response->getPackages() as $package) {
    echo $package->getDisplayNameForLanguage('en')->getName(), "\n";
}

订阅

检索用户的订阅列表

$apiClient = new \Ixolit\Dislo\Client($httpClient);

$response = $apiClient->subscriptionGetAll($token);

foreach ($response->getSubscriptions() as $subscription) {
    print_r([
        'status' => $subscription->getStatus(),
        'active' => $subscription->isActive(),
        'startedAt' => $subscription->getStartedAt()->format('c'),
        'package' => $subscription->getCurrentPackage()->getDisplayNameForLanguage('en')->getName(),
        'price (EU)' => $subscription->getCurrentPeriod()->getBasePriceForCurrency('EUR')->getAmount(),
        'metaData' => $subscription->getProvisioningMetaData(),
    ]);
}

搜索 API

在 Ixoplan 的搜索数据库中运行参数化查询,将返回数据流式传输到文件资源。

$apiClient = new \Ixolit\Dislo\Client($httpClient);

$date = date('Y-m-d');
$file = fopen('/path/to/file', 'w');

$apiClient->exportStreamQuery(
    'select * from users where last_indexed_at > :_last(date)',
    ['last' => $date],
    $file
);