我复制了一些我写过一段时间的 PHP 来清理地址。在原始页面中,它存在于实时网络服务器上并且运行良好。当前脚本从我的开发机器上的命令行运行。CLI 脚本总是抛出“索引未定义”错误,但索引已定义,来自以下代码:
$url = 'http://production.shippingapis.com/ShippingAPI.dll?API=ZipCodeLookup&XML=';
$msg = '
<ZipCodeLookupRequest USERID="xxxxxxxxxx">
<Address ID="0"><FirmName></FirmName><Address1>' . $suite . '</Address1>
<Address2>' . $street . '</Address2>
<City>' . $city . '</City><State>' . $state . '</State>
</Address></ZipCodeLookupRequest>
';
//get the response from the USPS
$newurl = $url . urlencode($msg);
// echo $newurl;
$xml = $newurl;
$parser = xml_parser_create();
// open a file and read data
$fp = fopen($xml, 'r');
$xmldata = fread($fp, 4096);
xml_parse_into_struct($parser, $xmldata, $values);
xml_parser_free($parser);
//echo $xmldata;
//print_r($values);
if ($values[6][tag] === 'ZIP4') {
$street = $values[2][value];
$city = $values[3][value];
$state = $values[4][value];
$zip5 = $values[5][value];
$zip4 = $values[6][value];
}
else if ($values[7][tag] === 'ZIP4') {
$suite = $values[2][value];
$street = $values[3][value];
$city = $values[4][value];
$state = $values[5][value];
$zip5 = $values[6][value];
$zip4 = $values[7][value];
}
else {
$suite = '';
$street = '';
$city = '';
$state = '';
$zip5 = '';
$zip4 = '';
}
;
if ($values[2][tag] != 'ERROR') {
$verifiedBlock = ("
$suite . chr(13) . chr(10);
$street . chr(13) . chr(10);
$city $state $zip5 $zip4
");
}
else {
$verifiedBlock = ("
The address could not be verified
");
}
;
如果我做一个 $values 的 print_r,我会得到这个:
Array ( [0] => Array
(
[tag] => ZIPCODELOOKUPRESPONSE
[type] => open
[level] => 1
)
[1] => Array
(
[tag] => ADDRESS
[type] => open
[level] => 2
[attributes] => Array
(
[ID] => 0
)
)
[2] => Array
(
[tag] => ADDRESS1
[type] => complete
[level] => 3
[value] => FL 7
)
等 - 我已经确认 [6] 和 [7] 始终存在。然而,它总是在这些行和行上抛出“索引未定义”错误if ($values[2][tag] != 'ERROR')
。
有人可以告诉我我在这里缺少什么愚蠢,明显的东西吗?