bueffelsoft/cyclos-rest-api

Cyclos REST API 的 PHP 客户端

1.3.2 2023-06-26 15:55 UTC

This package is auto-updated.

Last update: 2024-09-26 19:27:18 UTC


README

Latest Stable Version License

请将此用作项目的依赖项,而不是编写自己的客户端。通过 swagger codegen(目前为 2020-05-06)自动生成不产生可用的结果。始终使用 Access-Token 而不是用户名//密码,以确保最佳安全性并轻松阻止单个客户端!

composer require bueffelsoft/cyclos-rest-api

ApiWrapper 使用方法

ApiWrapper 将 swagger codegen 生成的代码捆绑在一起以访问任何 API,它没有端点特定的函数/模型。

$config = Cyclos\Api\Configuration::getDefaultConfiguration()
    ->setHost('https://cyclos-domain.tld/{network/}api')
    ->setApiKey('Access-Client-Token', '{client-tag}{access-token}')
    // or via the session token if one already exists for the end user:
    //->setApiKey('Session-Token', '{session-token}')
;
$wrapper = new \Cyclos\Api\ApiWrapper($config);

// see createRequest() doc for more parameters
$req = $wrapper->createRequest('/auth');

// see doRequest() doc for more parameters and response type

try {
    $res = $wrapper->doRequest($req);
}
catch(\Exception $e) {
  // $e = \Cyclos\Api\ApiException | \GuzzleHttp\Exception\GuzzleException
}

var_dump($res);

CyclosWrapper 使用方法

虽然 ApiWrapper 并非特定于 Cyclos API,但 CyclosWrapper 是其子类,实现了访问某些端点的辅助函数。创建一个如上所示的配置对象,并使用它来创建一个新的 CyclosWrapper。将调用封装在 try-catch 中以检查表示失败的请求的异常。如果异常是 ApiException,则服务器响应了错误,例如,查看 CyclosWrapper::loginUser 的处理方式。

use Cyclos\Api\CyclosWrapper;

$wrapper = new CyclosWrapper($config);

// find all users in one or more groups
$users = $wrapper->getUsers(['climate_project']);

// get data for a single user
$user = $wrapper->getUser('username|userId');

// login a user to retrieve a sessionToken and the users data
// IP/remoteAddress is used to block the user after X failed tries
$data = $wrapper->loginUser('username|userId', 'password', $_SERVER['REMOTE_ADDR'] ?? 'cli');

// get all accounts for a given user, may be filtered by account type
// (e.g. to specify the currency) 
$accounts = $wrapper->getUserAccounts("username|userId", 'accountTypeId');

// get all advertisements, may be filtered by owner, status, custom fields, ...
$ads = $wrapper->getAdvertisements([
   'owner'        => "-310719030079881031",  
   'kind'         => 'webshop',
   'statuses'     => ['active'],
   'customFields' => 'anzeigenart:co2_compensation',
]);

// add an item with the given quantity to the current users shopping cart
$items = $wrapper->addItemToCart("-4922405048507268931", 1.2);

// get a list of all carts for the current user (one per seller & currency)
$carts = $res = $wrapper->getShoppingCarts();

// remove a shopping cart, to cancel the process
$remainingData = $wrapper->deleteShoppingCart('-4922405048507268930');

// get data required to complete the sale, returns the current
// users addresses, possible delivery methods & payment types etc.
$data = $wrapper->getCheckoutData('-4922405048507268930');

// checkout the given cart, using the fetched data, this creates an order, 
// waiting to be accepted by the seller
$remainingData = $wrapper->checkoutShoppingCart('-4922405048507268930', [
    'deliveryAddress' => $data['addresses'][0],
    'deliveryMethod' => $data['deliveryMethods'][0]['id'],
    'paymentType' => $data['paymentTypes'][0]['id'],
]);