aftership/tracking-sdk

The official AfterShip Tracking PHP API library

9.0.0 2024-10-25 03:04 UTC

This package is auto-updated.

Last update: 2024-10-25 03:07:23 UTC


README

This library allows you to quickly and easily use the AfterShip Tracking API via PHP.

For updates to this library, see our GitHub release page.

If you need support using AfterShip products, please contact support@aftership.com.

Table of Contents

Before you begin

Before you begin to integrate:

API and SDK Version

Each SDK version is designed to work with a specific API version. Please refer to the table below to identify the supported API versions for each SDK version, ensuring you select the appropriate SDK version for the API version you intend to use.

Quick Start

Installation

composer require aftership/tracking-sdk

Constructor

Create AfterShip instance with options

Example

<?php

require_once __DIR__ . '/vendor/autoload.php';

$client = new \Tracking\Client([
    'apiKey' => 'YOUR_API_KEY',
    'authenticationType' => \Tracking\Config::AUTHENTICATION_TYPE_API_KEY,
]);

try {
    $trackingInfo = $client->tracking->getTrackingById('<tracking-id>');
    var_dump($trackingInfo->checkpoints);
} catch (\Tracking\Exception\AfterShipError $e) {
    // Handle the error
    var_dump($e->getErrorCode());
    var_dump($e->getStatusCode());
}

Rate Limiter

See the Rate Limit to understand the AfterShip rate limit policy.

Error Handling

The SDK will return an error object when there is any error during the request, with the following specification:

Error List

Endpoints

The AfterShip instance has the following properties which are exactly the same as the API endpoints:

  • courier - Get a list of our supported couriers.
  • tracking - Create trackings, update trackings, and get tracking results.
  • estimated-delivery-date - Get estimated delivery date for your order.

/trackings

POST /trackings

$payload = new \Tracking\API\Tracking\CreateTrackingRequest();
$payload->tracking_number = '<tracking_number>';
$payload->slug = '<slug>';
$trackingInfo = $client->tracking->createTracking($payload);
var_dump($trackingInfo);

DELETE /trackings/:id

$trackingInfo = $client->tracking->deleteTrackingById('<tracking_id>');
var_dump($trackingInfo);

GET /trackings

$query = new \Tracking\API\Tracking\GetTrackingsQuery();
$query->setPage(1)
    ->setLimit(10)
    ->setKeyword('123');
$trackingInfo = $client->tracking->getTrackings($query);
var_dump($trackingInfo);

GET /trackings/:id

$trackingInfo = $client->tracking->getTrackingById('<tracking_id>');
var_dump($trackingInfo);

PUT /trackings/:id

$payload = new \Tracking\API\Tracking\UpdateTrackingByIdRequest();
$payload->title = 'test';
$trackingInfo = $client->tracking->updateTrackingById('<tracking_id>', $payload);
var_dump($trackingInfo);

POST /trackings/:id/retrack

$trackingInfo = $client->tracking->retrackTrackingById('<tracking_id>');
var_dump($trackingInfo);

POST /trackings/:id/mark-as-completed

$payload = new \Tracking\API\Tracking\MarkTrackingCompletedByIdRequest();
$payload->reason = 'DELIVERED';
$trackingInfo = $client->tracking->markTrackingCompletedById('<tracking_id>', $payload);
var_dump($trackingInfo);

/couriers

GET /couriers

$couriers = $client->courier->getUserCouriers();
var_dump($couriers);

GET /couriers/all

$couriers = $client->courier->getAllCouriers();
var_dump($couriers);

POST /couriers/detect

$payload = new \Tracking\API\Courier\DetectCourierRequest();
$payload->slug = ['<slug>'];
$payload->tracking_number = '<tracking_number>';
$couriers = $client->courier->detectCourier($payload);
var_dump($couriers);

/estimated-delivery-date

POST /estimated-delivery-date/predict-batch

$payload = new \Tracking\API\EstimatedDeliveryDate\PredictBatchRequest();
$edd = new \Tracking\Model\EstimatedDeliveryDateRequest();
$edd->slug = '<slug>';
$edd->pickup_time = '2024-08-01 06:42:30';
$orginAddress = new \Tracking\Model\OriginAddressEstimatedDeliveryDateRequest();
$orginAddress->country = '<ISO 3166-1 country/region code>';
$orginAddress->state = '<ISO 3166-1 country/region code>';
$edd->origin_address = $orginAddress;

$destAddress = new \Tracking\Model\DestinationAddressEstimatedDeliveryDateRequest();
$destAddress->country = '<ISO 3166-1 country/region code>';
$destAddress->state = '<ISO 3166-1 country/region code>';
$edd->destination_address = $destAddress;
$payload->estimated_delivery_dates = [$edd];
$notification = $client->estimated_delivery_date->predictBatch($payload);
var_dump($notification);

Help

If you get stuck, we're here to help:

  • Issue Tracker for questions, feature requests, bug reports and general discussion related to this package. Try searching before you create a new issue.
  • Contact AfterShip official support via support@aftership.com

License

Copyright (c) 2024 AfterShip

Licensed under the MIT license.