UPS Tracking API - JSON body format?

Posting the answer to my own question answered by UPS support.

You'll POST the following json as the body content to the following API endpoint:

https://onlinetools.ups.com/json/Track

{
  "Security": {
    "UsernameToken": {
      "Username": "ups_username_here",
      "Password": "ups_password_here"
    },
    "UPSServiceAccessToken": {
      "AccessLicenseNumber": "ups_access_license_number_here"
    }
  },
  "TrackRequest": {
    "Request": {
      "RequestAction": "Track",
      "RequestOption": "activity"
    },
    "InquiryNumber": "1Z202Y36A898759591"
  }
}


On https://www.ups.com/track/api/Track/GetStatus you can post a json like

{
  "Locale": "da_DK",
  "TrackingNumber": ["enter your number here" ]
}

and you'll get a nice json structure back showing the current and previous location of the parcel

If you post multiple trackingnumbers, you only gets the summary for each parcel.


Here is the PHP code that I used to make the request, using cURL:

<?php

$url = 'https://wwwcie.ups.com/rest/Track'; // TEST
// $url = 'https://onlinetools.ups.com/rest/Track'; // PROD

$ups_array = array(
    'UPSSecurity' => array(
        'UsernameToken' => array(
            'Username' => 'ups_username_here',
            'Password' => 'ups_password_here'
        ),
        'ServiceAccessToken' => array(
            'AccessLicenseNumber' => 'ups_access_license_number_here'
        )
    ),
    'TrackRequest' => array(
        'Request' => array(
            'RequestOption' => 1,
            'TransactionReference' => array(
                'CustomerContext' => 'Test 001'
            )
        ),
        'InquiryNumber' => 'ups_package_tracking_number_here' //'1ZE1XXXXXXXXXXXXXX'
    )
);

$params = json_encode($ups_array);  // TURN THE ARRAY INTO JSON

$results_json = scrape_page($url, $params);  // POST TO THE API

$results = json_decode($results_json, true);  // DECODE THE JSON AND TURN IT INTO AN ARRAY

print "<PRE><FONT COLOR=RED>"; print_r($results); print "</FONT></PRE>";

Then, for the actual cURL function, I have this:

function scrape_page($url, $params) {

    $headers = array();
    $headers[] = 'Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept';
    $headers[] = 'Access-Control-Allow-Methods: POST';
    $headers[] = 'Access-Control-Allow-Origin: *';
    $headers[] = 'Content-Type: application/json';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,5);
    curl_setopt($ch, CURLOPT_TIMEOUT, 45);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    $response = curl_exec($ch);

    // CHECK TO SEE IF WE GOT AN ERROR  
    // IF SO, FORMAT IT LIKE THIS   ::28::Operation timed out afterseconds  
    if ((curl_errno($ch)) && (curl_errno($ch) != 0)) {
        $response = "::".curl_errno($ch)."::".curl_error($ch);
    }

    // SEND THE RESPONSE BACK TO THE SCRIPT
    return $response;

}

Tags:

Ups