[英]USPS API for tracking shipments in PHP
I have a problem. 我有个问题。 I registered on the USPS website and from there I took user and pass to access their API.
我在USPS网站上注册,从那里我拿了用户并通过访问他们的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
但是当我尝试访问跟踪时,我得到的结果是一个包含所有结果的单个文本字符串,而没有格式化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
您的物品于2015年6月5日上午10:51在意大利交付。海关清关处理完成,2015年6月4日,下午5:29,ITALYCustoms Clearance,2015年6月4日,上午11:20,意大利通过分拣设施进行处理, 2015年6月4日,上午11:19,ITALYDeparted,2015年6月3日,上午7:43,米兰,意大利出发,2015年6月2日,下午3:34,迈阿密,美国抵达,2015年6月2日,上午10:05 ,迈阿密,美国2015年6月1日晚上8:07分,通过分拣设施,ISC迈阿密佛罗里达大学(USPS)于2015年6月1日下午8:07抵达分拣设施,ISC MIAMI FL(USPS)离开USPS设施, 2015年6月1日,凌晨2点40分,佛罗里达州迈阿密33112抵达美国邮政总局设施,2015年5月31日上午11点22分,佛罗里达州迈阿密33112离开邮局,2015年5月30日下午4:07,佛罗里达州博卡拉顿33431Acceptance,2015年5月30日,下午12:27,BOCA RATON,FL 33431预计发货信息已发送至USPS,2015年5月29日
How can I get an XML and then extrapolate individual information I need? 如何获取XML然后推断我需要的个人信息? I can not jump out.
我不能跳出来。
You have to build your xml response: 您必须构建xml响应:
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
?>
The XML tags are being interpreted by your browser as HTML tags. 您的浏览器将XML标记解释为HTML标记。 Escape special characters like
<
with htmlspecialchars
if you're looking to echo the XML out to the user: 如果您希望将XML回显给用户,请转义
<
with htmlspecialchars
等特殊字符:
echo htmlspecialchars($deliveryStatus);
If it would be helpful for someone: 如果它对某人有帮助:
$deliveryStatus = $response->TrackInfo->TrackSummary;
echo $deliveryStatus;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.