PHP code example of vt-parhomenko / olx-ua-api-sdk

1. Go to this page and download the library: Download vt-parhomenko/olx-ua-api-sdk 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/ */

    

vt-parhomenko / olx-ua-api-sdk example snippets




use Parhomenko\Olx\OlxFactory;
use Parhomenko\Olx\Country;

$olx = OlxFactory::get(Country::UA, [
    'client_id'     => 'your-client-id',
    'client_secret' => 'your-client-secret',
    'access_token'  => 'stored-access-token',
    'refresh_token' => 'stored-refresh-token',
    'token_type'    => 'Bearer',
    'expires_in'    => 86400,
    'updated_at'    => '2026-05-30T12:00:00+03:00', // ISO-8601 (offset-aware); when issued/stored
], $update_token = true); // auto-refresh the access token if it has expired

// Create an advert
$advert = $olx->adverts()->create([
    // ...advert payload, see the OLX API docs
]);

use GuzzleHttp\HandlerStack;
use Parhomenko\Olx\Country;
use Parhomenko\Olx\OlxFactory;

$stack = HandlerStack::create();
$stack->push($myLoggingOrRetryMiddleware); // any Guzzle middleware

$olx = OlxFactory::get(Country::UA, $credentials, false, [
    'timeout'         => 30,   // total seconds per request
    'connect_timeout' => 5,    // seconds to establish the connection
    'handler'         => $stack,
]);

$olx->getHttpClient(); // the configured GuzzleHttp\ClientInterface

use Parhomenko\Olx\OlxFactory;
use Parhomenko\Olx\Country;

$olx = OlxFactory::get(Country::UA, [
    'client_id'     => 'your-client-id',
    'client_secret' => 'your-client-secret',
]);

// 1. Redirect the user to the OLX consent screen
$url = $olx->authenticator()->getOAuthLink('https://your-app.test/callback', 'optional-state');
header('Location: ' . $url);

// 2. On your callback, exchange the received ?code for tokens
$auth = $olx->authenticator()->authorize($_GET['code'], 'https://your-app.test/callback');

// 3. Persist the tokens for later use
$tokens = [
    'access_token'  => $auth->getAccessToken(),
    'refresh_token' => $auth->getRefreshToken(),
    'token_type'    => $auth->getTokenType(),
    'expires_in'    => $auth->getTokenExpiresIn(),
    'updated_at'    => $auth->getTokenUpdatedAt(), // ISO-8601, set by the SDK on authorize/refresh
];

// Refresh only when needed (compares updated_at + expires_in to now)
$olx->authenticator()->checkToken();

// Or force a refresh
$olx->authenticator()->refreshToken();

$olx->authenticator()->authenticateAsClient(); // grant_type=client_credentials
$categories = $olx->categories()->getAll();

$olx->adverts()->get(int $id): array;
$olx->adverts()->getAll(int $offset = 0, ?int $limit = null, ?string $external_id = null, string $category_ids = ''): array;
$olx->adverts()->create(array $params): array;
$olx->adverts()->update(int $id, array $params): array;
$olx->adverts()->activate(int $id): void;
$olx->adverts()->deactivate(int $id, bool $is_success = true): void;
$olx->adverts()->delete(int $id): void; // single DELETE request
$olx->adverts()->statistics(int $id): array;
$olx->adverts()->deleteStatistic(int $id, string $statistic_name): void;
$olx->adverts()->moderationReason(int $id): array; // why a rejected advert was refused
$olx->adverts()->logos(int $id): array;
$olx->adverts()->addLogo(int $id, array $params): array;
$olx->adverts()->deleteLogo(int $id, int $logo_id): void;

$olx->categories()->get(int $category_id): array;
$olx->categories()->getAll(int $parent_id = 0): array;
$olx->categories()->attributes(int $category_id): array;
$olx->categories()->suggestion(string $q): array; // suggest categories by advert title (min. 3 chars)

$olx->cities()->getAll(int $offset = 0, ?int $limit = null): array;
$olx->cities()->get(int $city_id): array;
$olx->cities()->districts(int $city_id): array; // districts of a city

$olx->regions()->getAll(): array;
$olx->regions()->get(int $region_id): array;

$olx->districts()->getAll(): array;
$olx->districts()->get(int $district_id): array;

$olx->locations()->getByCoordinates(float $latitude, float $longitude): array;

$olx->currencies()->getAll(int $offset = 0, ?int $limit = null): array;
$olx->languages()->getAll(): array;

$olx->users()->me(): array;            // the authenticated user
$olx->users()->get(int $user_id): array;
$olx->users()->accountBalance(): array;   // wallet / bonus / refund credits
$olx->users()->paymentMethods(): array;   // available methods (account, postpaid)
$olx->users()->billing(int $page = 1, ?int $limit = null): array;
$olx->users()->prepaidInvoices(int $page = 1, ?int $limit = null): array;
$olx->users()->postpaidInvoices(int $page = 1, ?int $limit = null): array;

$olx->packets()->getAll(int $category_id, string $payment_method, ?string $type = null, ?bool $with_features = null, ?int $zone_id = null): array;
$olx->packets()->zones(int $category_id): array;
$olx->packets()->userPackets(int $offset = 0, ?int $limit = null, ?string $availability = null, ?string $sort_by = null): array;
$olx->packets()->buyForUser(array $params): array;            // POST /users/me/packets
$olx->packets()->buyForAdvert(int $advert_id, array $params): array; // POST /adverts/{id}/packets

$olx->paidFeatures()->getAll(): array;                          // promotions, top ads, …
$olx->paidFeatures()->forAdvert(int $advert_id): array;
$olx->paidFeatures()->buyForAdvert(int $advert_id, array $params): array; // {code, payment_method}

$olx->usersBusiness()->me(): array;
$olx->usersBusiness()->update(array $params): array;
$olx->usersBusiness()->logos(): array;
$olx->usersBusiness()->addLogo(array $params): array;
$olx->usersBusiness()->deleteLogo(int $logo_id): void;
$olx->usersBusiness()->banners(): array;
$olx->usersBusiness()->addBanner(array $params): array;
$olx->usersBusiness()->deleteBanner(int $banner_id): void;

$olx->threads()->get(int $thread_id): array;
$olx->threads()->getAll(int $offset = 0, ?int $limit = null, ?int $advert_id = null, ?int $interlocutor_id = null): array;
$olx->threads()->markAsRead(int $thread_id): void;
$olx->threads()->setFavourite(int $thread_id): void;
$olx->threads()->unsetFavourite(int $thread_id): void;
$olx->threads()->post(int $thread_id, string $text): void;

$olx->messages()->get(int $thread_id, int $offset = 0, ?int $limit = null): array;
$olx->messages()->getOne(int $thread_id, int $message_id): array;

$balance = $olx->users()->accountBalance();        // "Not enough credits"
$methods = $olx->users()->paymentMethods();         // is "postpaid" activated?
$packets = $olx->packets()->getAll($categoryId, 'account'); // packet variant exists?

$olx->packets()->buyForAdvert($advertId, ['payment_method' => 'account']);

use Parhomenko\Olx\Exceptions\ValidationException;
use Parhomenko\Olx\Exceptions\CallLimitException;
use Parhomenko\Olx\Exceptions\BaseOlxException;

try {
    $olx->adverts()->create($payload);
} catch (ValidationException $e) {
    $errors = $e->getValidation();
} catch (CallLimitException $e) {
    // back off and retry later
} catch (BaseOlxException $e) {
    // any other OLX API error
    error_log($e->getTitle() . ': ' . $e->getDetail());
}

// 2.x
OlxFactory::get('ua', $credentials);

// 3.0 — positional string calls still work, enum is recommended
OlxFactory::get(Country::UA, $credentials);
OlxFactory::get('ua', $credentials); // still valid

// 2.x
$olx->adverts()->delete_notactive($id); // DELETE only
$olx->adverts()->delete($id);           // deactivated, then deleted

// 3.0
$olx->adverts()->delete($id);     // single DELETE (replaces delete_notactive)

$olx->adverts()->deactivate($id); // call explicitly if you need the old
$olx->adverts()->delete($id);     // deactivate-then-delete behaviour

// 2.x
$olx->user()->getOAuthLink(...);
$olx->user()->refreshToken();

// 3.0
$olx->authenticator()->getOAuthLink(...);
$olx->authenticator()->refreshToken();

// before
if ($olx->adverts()->activate($id)) { ... }

// 3.0 — success = no exception thrown
$olx->adverts()->activate($id);

use Parhomenko\Olx\Credentials;

$olx = OlxFactory::get(Country::UA, new Credentials(
    clientId: 'your-client-id',
    clientSecret: 'your-client-secret',
));