Pergunta

I copied some PHP I wrote a while back to scrub addresses. In the original page, it lives on a live webserver and works perfectly. The current script runs from the command line on my development machine. the CLI script always throws 'index not defined' errors, but the index is defined, from this code:

$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
    ");
}
;

If I do a print_r of $values, I get back this:

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
       )

etc - I have confirmed that [6] and [7] always exist. Yet, always it throws 'index not defined' errors on these lines and the if ($values[2][tag] != 'ERROR') line.

Can someone please tell me what stupid, obvious thing I'm missing here?

Foi útil?

Solução

In your code I see $values[2][tag].In this context tag is a constant that must not be defined. You've got key tag that is a string, so you have to use it as string, e.g. $values[2]['tag']. Oh, and do the same thing with other indexes (keys).

Outras dicas

change it to :

if ($values[2]["tag"] != 'ERROR')
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top