Reputation: 39
I have a problem. I registered on the USPS website and from there I took user and pass to access their API. But when I try to access the tracking I get as a result a single string of text with all the result without formatting XML
The script that I use is this:
$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 I get is
Your item was delivered in ITALY at 10:51 am on June 5, 2015.Customs clearance processing complete, June 4, 2015, 5:29 pm, ITALYCustoms Clearance, June 4, 2015, 11:20 am, ITALYProcessed Through Sort Facility, June 4, 2015, 11:19 am, ITALYDeparted, June 3, 2015, 7:43 am, Milan, ITALYDeparted, June 2, 2015, 3:34 pm, Miami, UNITED STATESArrived, June 2, 2015, 10:05 am, Miami, UNITED STATESProcessed Through Sort Facility, June 1, 2015, 8:07 pm, ISC MIAMI FL (USPS)Arrived at Sort Facility, June 1, 2015, 8:07 pm, ISC MIAMI FL (USPS)Departed USPS Facility, June 1, 2015, 2:40 am, MIAMI, FL 33112Arrived at USPS Origin Facility, May 31, 2015, 11:22 am, MIAMI, FL 33112Departed Post Office, May 30, 2015, 4:07 pm, BOCA RATON, FL 33431Acceptance, May 30, 2015, 12:27 pm, BOCA RATON, FL 33431Pre-Shipment Info Sent to USPS, May 29, 2015
How can I get an XML and then extrapolate individual information I need? I can not jump out.
Upvotes: 3
Views: 3097
Reputation: 27
If it would be helpful for someone:
$deliveryStatus = $response->TrackInfo->TrackSummary;
echo $deliveryStatus;
Upvotes: 0
Reputation: 2058
You have to build your xml response:
Something like this:
<?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
?>
Upvotes: 1
Reputation: 180137
The XML tags are being interpreted by your browser as HTML tags. Escape special characters like <
with htmlspecialchars
if you're looking to echo the XML out to the user:
echo htmlspecialchars($deliveryStatus);
Upvotes: 1