1. Go to this page and download the library: Download andy87/php-client-avito library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
andy87 / php-client-avito example snippets
declare(strict_types=1);
iClientAvito;
use and_y87\php_client_avito\Generated\Prompt\GetUserInfoSelfPrompt;
$client = new ApiClientAvito([
'clientId' => 'your-client-id',
'clientSecret' => 'your-client-secret',
]);
$prompt = new GetUserInfoSelfPrompt();
$response = $client->user->getUserInfoSelf($prompt);
echo $response->id . PHP_EOL;
echo $response->name . PHP_EOL;
use and_y87\php_client_avito\Generated\Prompt\VasPricesPrompt;
$prompt = new VasPricesPrompt();
$prompt->itemIds = [123456789, 987654321];
$response = $client->item->vasPrices($prompt);
use and_y87\php_client_avito\Generated\Prompt\GetUserInfoSelfPrompt;
$prompt = new GetUserInfoSelfPrompt();
$response = $client->user->getUserInfoSelf($prompt);
declare(strict_types=1);
use and_y87\PhpClientSdk\Client\Event\BeforeRequestEvent;
use and_y87\php_client_avito\ApiClientAvito;
$client = new ApiClientAvito($config, [
ApiClientAvito::EVENTS => [
ApiClientAvito::EVENT_BEFORE_REQUEST => [
static function (BeforeRequestEvent $event): void {
$event->request->headers['X-Trace-Id'] = bin2hex(random_bytes(8));
},
static function (BeforeRequestEvent $event): void {
$event->request->query['debug'] = '1';
},
],
],
]);
use and_y87\php_client_avito\ApiClientAvito;
$client = new ApiClientAvito($config, [
ApiClientAvito::REFRESH_AUTHORIZATION_STATUS_CODES => [401],
]);
$clientWithoutRefreshRetry = new ApiClientAvito($config, [
ApiClientAvito::REFRESH_AUTHORIZATION_STATUS_CODES => [],
]);
use and_y87\php_client_avito\ApiClientAvito;
use and_y87\PhpClientSdk\Transport\Cache\ArrayCache;
$client = new ApiClientAvito($config, [
ApiClientAvito::TOKEN_CACHE => new ArrayCache(),
ApiClientAvito::TOKEN_CACHE_KEY => 'avito:oauth:client-id',
ApiClientAvito::TOKEN_CLOCK_SKEW => 60,
]);
use and_y87\php_client_avito\ApiClientAvito;
use and_y87\php_client_avito\Generated\Prompt\GetUserInfoSelfPrompt;
use and_y87\PhpClientSdk\Security\Authorization\Strategy\ApiKeyAuthorizationStrategy;
use and_y87\PhpClientSdk\Security\Authorization\Resolver\PromptClassAuthorizationStrategyResolver;
$client = new ApiClientAvito($config, [
ApiClientAvito::AUTHORIZATION_RESOLVER => new PromptClassAuthorizationStrategyResolver([
GetUserInfoSelfPrompt::class => new ApiKeyAuthorizationStrategy('X-Api-Key', 'secret'),
]),
]);
use and_y87\php_client_avito\ApiClientAvito;
use and_y87\php_client_avito\Generated\Prompt\GetUserInfoSelfPrompt;
$client = new ApiClientAvito($config, [
ApiClientAvito::TRACEABLE_TRANSPORT => true,
]);
$response = $client->user->getUserInfoSelf(new GetUserInfoSelfPrompt());
$lastRecord = $client->getTraceableTransport()?->getLastRecord();
declare(strict_types=1);
use and_y87\php_client_avito\ApiClientAvito;
use and_y87\php_client_avito\Generated\Prompt\GetUserInfoSelfPrompt;
use and_y87\PhpClientSdk\Security\Authorization\Strategy\NullAuthorizationStrategy;
use and_y87\PhpClientSdk\Testing\Mock\MockTransport;
use and_y87\PhpClientSdk\Testing\Mock\PromptClassMockResponseResolver;
$resolver = (new PromptClassMockResponseResolver())
->addJson(GetUserInfoSelfPrompt::class, [
'id' => 123,
'name' => 'Mock User',
]);
$client = new ApiClientAvito(
[
'clientId' => 'test-client-id',
'clientSecret' => 'test-client-secret',
'baseUrl' => 'https://api.avito.test',
],
new MockTransport($resolver),
new NullAuthorizationStrategy(),
);
$response = $client->user->getUserInfoSelf(new GetUserInfoSelfPrompt());
use and_y87\php_client_avito\ApiClientAvito;
$client = new ApiClientAvito([
'clientId' => 'your-client-id',
'clientSecret' => 'your-client-secret',
]);
print_r($client->providerNames());