RetailCRM API Client

CustomMethods extends AbstractApiResourceGroup

Class CustomMethods

Tags
category

CustomMethods

SuppressWarnings

(PHPMD.CouplingBetweenObjects)

Table of Contents

Methods

__call()  : array<int|string, mixed>|object
Calls custom method, returns array response.
call()  : array<int|string, mixed>|object
Calls custom method, returns array response.
register()  : void
Register custom method in the resource group. You can use invokable class for easier implementation.

Methods

__call()

Calls custom method, returns array response.

public __call(string $name, array<int|string, mixed> $arguments) : array<int|string, mixed>|object
Parameters
$name : string
$arguments : array<int|string, mixed>
Tags
throws
ApiExceptionInterface
throws
ClientExceptionInterface
throws
AccountDoesNotExistException
throws
ApiErrorException
throws
MissingCredentialsException
throws
MissingParameterException
throws
ValidationException
throws
HandlerException
throws
HttpClientException
see
CustomMethods::call()
Return values
array<int|string, mixed>|object

call()

Calls custom method, returns array response.

public call(string $name[, array<int|string, mixed>|object $data = [] ][, array<int|string, mixed> $context = [] ]) : array<int|string, mixed>|object

Usage:

$client->customMethods->call('scopes');

Second parameter should be provided for POST requests or query data. Third parameter is used to pass anything you like to the callable during the method call (logger, for example).

This implementation is also used in the __call magic method. It works like this:

$client->customMethods->scopes($data, 'any', 'other', 'params');

will make this call:

$client->customMethods->call('scopes', $data, ['any', 'other', 'params']);

Full example for the settings method:

use RetailCrm\Api\Component\CustomApiMethod;
use RetailCrm\Api\Enum\RequestMethod;
use RetailCrm\Api\Factory\SimpleClientFactory;
use RetailCrm\Api\Interfaces\ApiExceptionInterface;

$client = SimpleClientFactory::createClient('https://azgalot.retailcrm.ru', '9y3e3ohX1NGqAausgg5ACMWPv5Z4iXQF');
$client->customMethods->register('settings', new CustomApiMethod(RequestMethod::GET, 'settings'));

try {
    // It will work because 'settings' method was registered before
    $settings = $client->customMethods->settings();
} catch (ApiExceptionInterface $exception) {
    echo sprintf(
        'Error from RetailCRM API (status code: %d): %s',
        $exception->getStatusCode(),
        $exception->getMessage()
    );

    if (count($exception->getErrorResponse()->errors) > 0) {
        echo PHP_EOL . 'Errors: ' . implode(', ', $exception->getErrorResponse()->errors);
    }

    return;
}

echo 'Timezone: ' . $settings['settings']['timezone']['value'];
Parameters
$name : string
$data : array<int|string, mixed>|object = []
$context : array<int|string, mixed> = []
Tags
throws
ApiExceptionInterface
throws
ClientExceptionInterface
throws
AccountDoesNotExistException
throws
ApiErrorException
throws
MissingCredentialsException
throws
MissingParameterException
throws
ValidationException
throws
HandlerException
throws
HttpClientException
Return values
array<int|string, mixed>|object

register()

Register custom method in the resource group. You can use invokable class for easier implementation.

public register(string $name, callable|CustomApiMethod $sender) : void

Registered callable should accept three arguments:

  • RequestSenderInterface
  • Array with the request data (may be null or empty if not provided - this is the case for get requests).
  • Context data - may contain any data. You can use the context for anything you like.

Example with the CustomApiMethod wrapper:

use RetailCrm\Api\Component\CustomApiMethod;
use RetailCrm\Api\Enum\RequestMethod;
use RetailCrm\Api\Factory\SimpleClientFactory;
use RetailCrm\Api\Interfaces\ApiExceptionInterface;

$client = SimpleClientFactory::createClient('https://test.simla.com', 'apiKey');
$client->customMethods->register('settings', new CustomApiMethod(RequestMethod::GET, 'settings'));

Sometimes CustomApiMethod may feel too simple to do certain tasks in the methods. That's why it is possible to register custom callable. Let's register a custom method that returns array of available scopes:

use RetailCrm\Api\Enum\RequestMethod; use RetailCrm\Api\Factory\SimpleClientFactory; use RetailCrm\Api\Interfaces\ApiExceptionInterface; use RetailCrm\Api\Interfaces\RequestSenderInterface;

$client = SimpleClientFactory::createClient('https://test.simla.com', 'apiKey');
$client->customMethods->register(
    'scopes',
    static function (RequestSenderInterface $sender, $data, array $context) {
        return $sender->send(
            RequestMethod::GET,
            sprintf('https://%s/api/credentials', $sender->host()),
            $data
        )['scopes'];
    }
);

Check call() method to learn how to call registered methods.

Parameters
$name : string
$sender : callable|CustomApiMethod
Tags
see
self::call
see
RequestSenderInterface
see
RequestSender
see
CustomApiMethod

        
On this page

Search results