我正在对当前的运输管理系统进行非常小的更新。我应该构建一个PHP脚本,以使用USPS的AddressValidate API实质上使用给定的地址,并通过USPS对其进行验证。当然,USPS将返回格式正确的地址作为数组。我的问题是,如何将每个字段的返回值转换为变量? (即地址1,地址2,城市等),然后按以下方式回显结果
地址1: Happyville Lane 123号
城市:匹兹堡
我当前的脚本:(我的详细信息已用*遮盖)
<?php
$user = '****';
$xml_data = "<AddressValidateRequest USERID='$user'>" .
"<IncludeOptionalElements>true</IncludeOptionalElements>" .
"<ReturnCarrierRoute>true</ReturnCarrierRoute>" .
"<Address ID='0'>" .
"<FirmName />" .
"<Address1>123 happyville lane</Address1>" .
"<Address2></Address2>" .
"<City>columbus</City>" .
"<State>ohio</State>" .
"<Zip5></Zip5>" .
"<Zip4></Zip4>" .
"</Address>" .
"</AddressValidateRequest>";
$url = "http://production.shippingapis.com/ShippingAPI.dll?API=Verify";
//setting the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// Following line is compulsary to add as it is:
curl_setopt($ch, CURLOPT_POSTFIELDS,
'XML=' . $xml_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
$output = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
print_r('<pre>');
print_r($array_data);
print_r('</pre>');
echo PHP_EOL;
返回的数组如下:
Array
(
[Address] => Array
(
[@attributes] => Array
(
[ID] => 0
)
[Address2] => 123 HAPPYVILLE LANE
[City] => COLUMBUS
[State] => OH
[Zip5] => 12345
[Zip4] => 1849
[DeliveryPoint] => 18
[CarrierRoute] => AC016
)
)
答案 0 :(得分:0)
echo "Address1: ".$array_data["Address"]["Address2"]."<br>";
echo "City: ".$array_data["Address"]["City"] //wrong city in example :P
应该可以解决问题 或如果还有更多地址:
foreach($array_data as $key=>$address){
$address2=$array_data["Address"]["Address2"];//if you need the assignment
echo "Address1: ".$address2."<br>";
echo "City: ".$array_data["Address"]["City"] //wrong city in example :P
}