USPS API for tracking shipments in PHP

I have a problem. I registered on the USPS website and from there I took the user and passed it to the API. But when I try to access the tracking, I end up with a single line of text with the entire result without XML formatting

The script I'm using:

    $trackingNumber = $numtrack;
    $url = "http://production.shippingapis.com/shippingAPI.dll";
    $service = "TrackV2";
    $xml = rawurlencode("<TrackRequest USERID='MYIDACCOUNT'><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);
    curl_close($ch);

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

      

The result is

Your item was shipped to Italy at 10:51 am on June 5, 2015. Customs completion of processing, June 4, 2015, 17:29, ITALY Clearance, June 4, 2015, 11:20 AM, ITALY Processed through sorting Institution, June 4, 2015, 11:19, ITALY, June 3, 2015, 7: 43 am, Milan, ITALY, June 2, 2015, 3:34 pm, Miami, USA STATES Arrived, June 2, 2015, 10:05 am, Miami, UNITED STATES Processed Through Sorter, June 1, 2015, 8:07 pm. ISC MIAMI FL (USPS) Arrived for Sort, Jun 1, 2015 8:07 PM, ISC MIAMI FL (USPS) Retired from USPS Fund, Jun 1, 2015 2:40 AM, MIAMI, FL 33112 Acquired from USPS Origin Facility. May 31, 2015 11:22 AM, MIAMI, FL 33112 Posted Post Office, May 30, 2015, 4:07 PM, BOCA RATON, FL 33431 Acceptance, May 30, 2015 12:27 PM, BOCA RATON, FL 33431 Before delivery information, sent to USPS on May 29, 2015.

How can I get the XML and then extrapolate the information I need? I can't jump out.

+3


source to share


3 answers


You have to create your XML request:

Something like that:



<?php
/*
SimpleXMLElement Object ( [TrackInfo] => SimpleXMLElement Object ( [@attributes] => Array ( [ID] => CV034836340US ) [TrackSummary] => Your item was delivered in ITALY at 10:51 am on June 5, 2015. [TrackDetail] => Array ( [0] => Customs clearance processing complete, June 4, 2015, 5:29 pm, ITALY [1] => Customs Clearance, June 4, 2015, 11:20 am, ITALY [2] => Processed Through Sort Facility, June 4, 2015, 11:19 am, ITALY... ) ) )
*/
//SIMULATION FROM YOUR OBJECT
$response = (object)array(
'TrackInfo'=>array('ID'=>12342432),
'TrackSummary'=>'Your item was delivered in ITALY at 10:51 am on June 5, 2015.',
'TrackDetail' => array('Customs clearance processing complete, June 4, 2015, 5:29 pm, ITALY','Customs Clearance, June 4, 2015, 11:20 am, ITALY','Processed Through Sort Facility, June 4, 2015, 11:19..., ITALY... ')
);


$string = '<?xml version="1.0" encoding="utf-8"?>
<TrackResponse>
    <TrackInfo>'.(string)$response->TrackInfo['ID'].'</TrackInfo>
    <TrackSummary>'.(string)$response->TrackSummary.'</TrackSummary>
    <TrackDetail>';
    foreach($response->TrackDetail as $detail){
        $string .= '<detail>'.(string)$detail.'</detail>';
    }
$string .='</TrackDetail>
</TrackResponse>';

header('Content-Type: application/xml; charset=utf-8');
echo $string
?> 

      

+1


source


XML tags are interpreted by your browser as HTML tags. Escape special characters like <

c htmlspecialchars

if you want to strip XML from the user:



echo htmlspecialchars($deliveryStatus);

      

+1


source


If it is useful to someone:

$deliveryStatus = $response->TrackInfo->TrackSummary;
echo $deliveryStatus;

      

0


source







All Articles