tibezh/ukrposhta-php-sdk 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tibezh/ukrposhta-php-sdk

Composer 安装命令:

composer require tibezh/ukrposhta-php-sdk

包简介

Contains integration with Ukrposhta service.

README 文档

README

Ukrposhta PHP SDK logo

An Ukrposhta PHP SDK based on the official Ukrposhta API.

Minimum PHP Version License CI codecov Latest Stable Version

Table of Contents

Requirements

This library uses PHP 8.3+.

To use the Ukrposhta API, you need to have Bearer and Token for each API sub-portal (eCom, StatusTracking and AddressClassifier). After signing the contract, the bearer and token are issued by your manager. You can find more information here.

Available Features

  • Status Tracking - available.
  • Address Classifier (counterparty) - available.
  • Shipments - planned.

Installation

To get started, simply require the project using Composer.

composer require tibezh/ukrposhta-php-sdk

Configuration

Retry Configuration

The SDK includes automatic retry logic for transient network errors (connection timeouts, DNS failures, etc.) with exponential backoff and jitter.

Default settings:

  • Max retries: 3 attempts
  • Base delay: 100ms (with exponential backoff: 100ms, 200ms, 400ms...)

You can customize retry behavior when creating a custom Request object:

use Ukrposhta\Request\Request;
use Ukrposhta\Tracking\Tracking;

// Create a custom request with retry settings.
$request = new Request(
    logger: null,       // Optional PSR-3 logger
    maxRetries: 5,      // Max retry attempts (default: 3)
    retryDelayMs: 200   // Base delay in milliseconds (default: 100)
);

// Use the custom request with Tracking.
$tracking = new Tracking(
    bearerStatusTracking: '[BEARER-TOKEN]',
    request: $request
);

To disable retries, set maxRetries to 0:

$request = new Request(logger: null, maxRetries: 0);

Logging

The SDK supports PSR-3 logging. Pass any PSR-3 compatible logger to track API requests and responses:

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Ukrposhta\Tracking\Tracking;

$logger = new Logger('ukrposhta');
$logger->pushHandler(new StreamHandler('path/to/ukrposhta.log', Logger::DEBUG));

$tracking = new Tracking(
    bearerStatusTracking: '[BEARER-TOKEN]',
    logger: $logger
);

Examples

Status Tracking

Request last status by barcode:

/** @var \Ukrposhta\Tracking\Entities\TrackingStatusInterface $barcodeLastStatus */
$barcodeLastStatus = (new \Ukrposhta\Tracking\Tracking())
  ->setAccessToken('[BEARER-STATUS-TRACKING-ACCESS-TOKEN]')
  // To get results in English:
  // ->setRequestLang('EN')
  ->requestBarcodeLastStatus('[BARCODE]');

// Prints event name value of the last status for the given barcode.
print $barcodeLastStatus->getEventName();

Request all statuses by barcode:

/** @var \Ukrposhta\Tracking\Entities\TrackingStatusCollectionInterface $barcodeStatuses */
$barcodeStatuses = (new \Ukrposhta\Tracking\Tracking())
  ->setAccessToken('[BEARER-STATUS-TRACKING-ACCESS-TOKEN]')
  // To get results in English:
  // ->setRequestLang('EN')
  ->requestBarcodeStatuses('[BARCODE]');

// Prints "[date]: [eventName]" of each status for the given barcode.
foreach ($barcodeStatuses as $status) {
  print $status->getDate()->format('c') . ': ' . $status->getEventName();
  print '<br>';
}

Request route by barcode:

/** @var \Ukrposhta\Tracking\Entities\TrackingRouteInterface $barcodeRoute */
$barcodeRoute = (new \Ukrposhta\Tracking\Tracking())
  ->setAccessToken('[BEARER-STATUS-TRACKING-ACCESS-TOKEN]')
  // To get results in English:
  // ->setRequestLang('EN')
  ->requestBarcodeRoute('[BARCODE]');

// Prints "[from] -> [to]" information for the given barcode.
print $barcodeRoute->getFrom() . ' -> ' . $barcodeRoute->getTo();

Address Classifier

The Address Classifier API allows you to search regions, districts, cities, streets, post offices and more.

Request regions:

use Ukrposhta\AddressClassifier\AddressClassifier;
use Ukrposhta\Utilities\Languages\LanguagesEnum;

$classifier = new AddressClassifier(
    bearerCounterparty: '[BEARER-COUNTERPARTY-ACCESS-TOKEN]'
);

/** @var \Ukrposhta\AddressClassifier\Entities\Region\RegionCollectionInterface $regions */
$regions = $classifier->requestRegions('Київ');

foreach ($regions->all() as $region) {
    print $region->getId() . ': ' . $region->getName();
    print '<br>';
}

Request districts by region ID:

/** @var \Ukrposhta\AddressClassifier\Entities\District\DistrictCollectionInterface $districts */
$districts = $classifier->requestDistrictsByRegionId(regionId: 1);

foreach ($districts->all() as $district) {
    print $district->getId() . ': ' . $district->getName();
    print '<br>';
}

Request cities by region ID and district ID:

/** @var \Ukrposhta\AddressClassifier\Entities\City\CityCollectionInterface $cities */
$cities = $classifier->requestCityByRegionIdAndDistrictId(
    regionId: 1,
    districtId: 5,
    nameUa: 'Бориспіль'
);

foreach ($cities->all() as $city) {
    print $city->getId() . ': ' . $city->getName()->getByLanguage(LanguagesEnum::UA);
    print '<br>';
}

Request streets by city ID:

/** @var \Ukrposhta\AddressClassifier\Entities\Street\StreetCollectionInterface $streets */
$streets = $classifier->requestStreetByRegionIdAndDistrictIdAndCityId(
    regionId: 1,
    districtId: 5,
    cityId: 100,
    nameUa: 'Головна'
);

foreach ($streets->all() as $street) {
    print $street->getId() . ': ' . $street->getName()->getByLanguage(LanguagesEnum::UA);
    print '<br>';
}

Request post offices by city ID:

/** @var \Ukrposhta\AddressClassifier\Entities\PostOffice\PostOfficeCollectionInterface $postOffices */
$postOffices = $classifier->requestPostOfficeByCityId(cityId: 100);

foreach ($postOffices->all() as $postOffice) {
    print $postOffice->getPostIndex() . ': ' . $postOffice->getName()->getByLanguage(LanguagesEnum::UA);
    print '<br>';
}

Request nearest post offices by geolocation:

/** @var \Ukrposhta\AddressClassifier\Entities\NearestPostOffice\NearestPostOfficeCollectionInterface $nearestPostOffices */
$nearestPostOffices = $classifier->requestNearestPostOffices(
    latitude: 50.4501,
    longitude: 30.5234,
    maxDistance: 1000 // meters
);

foreach ($nearestPostOffices->all() as $postOffice) {
    print $postOffice->getFilialName() . ' - ' . $postOffice->getDistance() . ' m';
    print '<br>';
}

Fuzzy search for cities:

/** @var \Ukrposhta\AddressClassifier\Entities\CitySearchItem\CitySearchItemCollectionInterface $cities */
$cities = $classifier->requestSearchCity(
    regionId: 1,
    districtId: 5,
    cityName: 'Борис', // partial name
    language: LanguagesEnum::UA,
    fuzzy: true
);

foreach ($cities->all() as $city) {
    print $city->getName() . ' (' . $city->getTypeName() . ')';
    print '<br>';
}

Working with Collections

All collection classes implement Countable and IteratorAggregate/Iterator interfaces, allowing you to:

// Get count of items.
$count = count($regions);
// Or use the count() method.
$count = $regions->count();

// Check if collection is empty.
if ($regions->isEmpty()) {
    echo 'No regions found';
}

// Iterate directly with foreach.
foreach ($regions as $region) {
    echo $region->getName();
}

// Get all items as array.
$allRegions = $regions->all();

tibezh/ukrposhta-php-sdk 适用场景与选型建议

tibezh/ukrposhta-php-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 11 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「Ukrpostha」 「Ukrpostha PHP」 「Ukrpostha PHP SDK」 「Ukrpostha SDK」 「Ukrpostha PHP API」 「Ukrpostha API」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tibezh/ukrposhta-php-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tibezh/ukrposhta-php-sdk 我们能提供哪些服务?
定制开发 / 二次开发

基于 tibezh/ukrposhta-php-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 30
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-30