Getting USPS Order Tracking Status Using PHP

An error occurred while trying to get USPS order status using the USPS tracking API.

However, when I run the code that I built based on the USPS manual, I get the following error: " 80040B19XML Syntax error: 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?

+4
source share
3 answers

, , , , , :

, PHP , - , ( , , 22 ). , , . , .

$xml, . , :

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

​​ . , , .

+9

. , :

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

XML , PHP curl simplexml_load_file

It will look like this:

<?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;
?>
0
source

Source: https://habr.com/ru/post/1524558/


All Articles