-1

我复制了一些我写过一段时间的 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')

有人可以告诉我我在这里缺少什么愚蠢,明显的东西吗?

4

2 回答 2

1

将其更改为:

if ($values[2]["tag"] != 'ERROR')
于 2012-05-12T12:04:41.070 回答
1

在您的代码中,我看到$values[2][tag].In 这种上下文tag是一个不得定义的常量。你有tag一个字符串的键,所以你必须将它用作字符串,例如$values[2]['tag']. 哦,对其他索引(键)做同样的事情。

于 2012-05-12T12:06:19.010 回答