Tracking shipments manually can be time-consuming. With PHP, you can automate this process for ST Courier, making it faster and more efficient. This guide walks you through creating and using a PHP script to track shipments effortlessly.

Important Note : Since there is no official API documentation for tracking ST Courier shipments, we have reverse-engineered the shipment tracking implementation on their website. This approach may become obsolete if the courier company updates its implementation.

PHP Code

Here’s the complete PHP code to track your ST Courier shipments. Simply copy and paste the code into your PHP environment and follow the usage instructions below.

<?php

class STCourier
{
    private static $trackUrl = 'https://stcourier.com/track/doCheck';
    private static $cookieJar = '';

    private static function validateAWB($awb)
    {
        return !empty($awb) && strlen($awb) == 11 && is_numeric($awb);
    }

    private static function parseTrackingTable($html)
    {
        libxml_use_internal_errors(true);
        $dom = new DOMDocument();
        $dom->loadHTML($html);

        // Check for error message
        $xpath = new DOMXPath($dom);
        $errorDiv = $xpath->query("//h4[@class='text-danger']");
        if ($errorDiv->length > 0) {
            return [
                'code' => 404,
                'msg' => trim($errorDiv->item(0)->textContent)
            ];
        }

        // Look for tracking table
        $table = $dom->getElementsByTagName('table')->item(0);
        if (!$table) {
            return [
                'code' => 500,
                'msg' => 'Tracking details not found'
            ];
        }

        // Define key mapping
        $keyMap = [
            'Current Status' => 'status',
            'Orgin SRC' => 'src',
            'Destination' => 'dest',
            'Consignment' => 'type',
            'Book Date/Time' => 'date_booking',
            'Delivery Date/Time' => 'date_delivery'
        ];

        $data = [];
        foreach ($table->getElementsByTagName('tr') as $row) {
            $cols = $row->getElementsByTagName('td');
            if ($cols->length == 2) {
                $key = trim($cols->item(0)->textContent);
                $value = trim($cols->item(1)->textContent);
                if (isset($keyMap[$key])) {
                    $data[$keyMap[$key]] = $value;
                }
            }
        }

        return [
            'code' => 200,
            'data' => $data
        ];
    }

    public static function track($awbNo)
    {
        if (!self::validateAWB($awbNo)) {
            return json_encode([
                'code' => 400,
                'msg' => 'Invalid AWB Number'
            ]);
        }

        self::$cookieJar = tempnam(sys_get_temp_dir(), 'cookie_');
        $boundary = '-----------------------------' . time();

        $ch = curl_init();
        $body = "--$boundary\r\n";
        $body .= "Content-Disposition: form-data; name=\"awb_no\"\r\n\r\n";
        $body .= "$awbNo\r\n";
        $body .= "--$boundary--\r\n";

        curl_setopt_array($ch, [
            CURLOPT_URL => self::$trackUrl,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => $body,
            CURLOPT_COOKIEFILE => self::$cookieJar,
            CURLOPT_COOKIEJAR => self::$cookieJar,
            CURLOPT_ENCODING => 'gzip, deflate, br',
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTPHEADER => [
                'Content-Type: multipart/form-data; boundary=' . $boundary,
                'Accept: */*',
                'X-Requested-With: XMLHttpRequest',
                'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
                'Origin: https://stcourier.com',
                'Referer: https://stcourier.com/track/shipment'
            ]
        ]);

        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        if ($response === false) {
            return json_encode(['code' => 500, 'msg' => curl_error($ch)]);
        }

        curl_close($ch);
        unlink(self::$cookieJar);

        $result = json_decode($response, true);
        if ($result['code'] == 200) {
            // Get tracking page HTML
            $ch = curl_init('https://stcourier.com/track/shipment');
            curl_setopt_array($ch, [
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_COOKIEFILE => self::$cookieJar
            ]);
            $html = curl_exec($ch);
            curl_close($ch);

            return json_encode(self::parseTrackingTable($html));
        }

        return $response;
    }
}

// CLI interface
if (php_sapi_name() === 'cli') {
    if ($argc > 1) {
        $awbNo = $argv[1];
        $response = STCourier::track($awbNo);
        echo $response . PHP_EOL;
    } else {
        echo "Usage: php track_awb.php <AWB_NUMBER>" . PHP_EOL;
        echo "php track_awb.php 58104209590" . PHP_EOL;
    }
}

Usage

Valid AWB Number Example

$ php track_awb.php 58104209590
{   "code":200,
    "data": {
        "status":"Delivered",
        "src":"TNMAA-GSB",
        "dest":"TNERD-QAY",
        "type":"Dox - 1 Nos",
        "date_booking":"18-12-2024 10:21 PM",
        "date_delivery":"19-12-2024 6:20 PM"
    }
}

Invalid AWB Number Example

$ php track_awb.php 58104209591
{"code":404,"msg":"Sorry, Invalid AWB Number..! (58104209591)"}

This PHP script makes it easy to track ST Courier shipments programmatically. As the implementation may change, stay updated with ST Courier’s tracking system to ensure continued functionality. Feel free to modify the script to suit your specific needs.