Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining USPS orders tracking status with PHP

Error is thrown when trying to retrieve the status of a USPS order using the USPS Tracking API.

However, when running the code I built based on the USPS manual, I am getting the following error: "80040B19XML Syntax Error: Please check the XML request to see if it can be parsed.USPSCOM::DoAuth"

Link to the manual: https://www.usps.com/business/web-tools-apis/track-and-confirm-v1-3a.htm

Here is my code:

$trackingNumber = 123456;
$url = "http://production.shippingapis.com/shippingAPI.dll";
$service = "TrackV2";
$xml = rawurlencode("
<TrackRequest USERID='MYID'>
    <TrackID ID=".$trackingNumber."></TrackID>
    </TrackRequest>");
$request = $url . "?API=" . $service . "&XML=" . $xml;
// send the POST values to USPS
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$request);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// parameters to post

$result = curl_exec($ch);
//var_dump($result);
curl_close($ch);

$response = new SimpleXMLElement($result);
//print_r($result);
$deliveryStatus = $response->TrackResponse->TrackInfo->Status;
echo $deliveryStatus;

What am I doing wrong?

like image 488
user2509541 Avatar asked Jan 30 '14 17:01

user2509541


People also ask

Does USPS have a tracking API?

The USPS Web Tools® API library gives ecommerce website shopping carts and shipping software access to valuable USPS® data—free of charge! You can check shipping rates, track packages, and schedule a package pickup all through USPS Web Tools.

Is there another way to track USPS?

While there is no such option as USPS tracking by address, you may use the Informed Delivery service as an alternative way to receive the tracking information.

How do I track a USPS package by name?

By setting up an Informed Delivery service with your USPS account, you can track your packages. Another option is to use the USPS Tracking Number included when you mail a parcel. You can't track a package with a name.


2 Answers

While I'm sure the original author resolved their issue by now, having come to this example and finding it didn't work I figured I would address the issues:

First thing to address is that PHP converts the tracking number into scientific notation if the tracking number is all numbers as in the example above (test tracking number I used was an all numeric string of 22 characters). So I encased the numbers in single quotes to treat it as a string instead of a number. This issue was only discovered after the next one was addressed.

for the $xml, the ID needs to be encased in double quotes. So the code should be:

$xml = rawurlencode("
<TrackRequest USERID='MYID'>
    <TrackID ID=\"".$trackingNumber."\"></TrackID>
    </TrackRequest>");

Making these two changes resolved the posters original issue. Hope this helps anyone who also stumbles here.

like image 106
Mike B. Avatar answered Sep 28 '22 03:09

Mike B.


For getting details from XML content, in PHP rather than using curl you can use simplexml_load_file

It will be like:

<?php
    $xml=simplexml_load_file('http://production.shippingapis.com/ShippingApi.dll?API=TrackV2&XML=<TrackFieldRequest USERID="MYID"><TrackID ID="'.$trackingNumber.'"></TrackID></TrackFieldRequest>') or die('Error: Cannot create object');
    echo $xml->TrackInfo->TrackSummary->Event."<br>";
    echo $xml->TrackInfo->TrackSummary->EventDate."<br>";
    echo $xml->TrackInfo->TrackSummary->EventTime;
?>
like image 34
Pradeep Kumar Avatar answered Sep 28 '22 04:09

Pradeep Kumar