I am using gabrielbull/ups-api for validating user address if a user provides an address I verify weather the address is correct or not I am using a got and getting a response which is quite difficult for me to access the data here is what I am getting in return

Ups\Entity\AddressValidationResponse Object
(
    [response:protected] => SimpleXMLElement Object
        (
            [ValidAddressIndicator] => SimpleXMLElement Object
                (
                )

            [AddressKeyFormat] => SimpleXMLElement Object
                (
                    [AddressLine] => 2290 SW 128TH AVE
                    [Region] => MIRAMAR FL 33027-2648
                    [PoliticalDivision2] => MIRAMAR
                    [PoliticalDivision1] => FL
                    [PostcodePrimaryLow] => 33027
                    [PostcodeExtendedLow] => 2648
                    [CountryCode] => US
                )

        )

    [requestAction:protected] => 1
)

This is the code I used

$address = new \Ups\Entity\Address();
$address->setAddressLine1('2290 SW 128th Street');
$address->setAddressLine2('');
$address->setStateProvinceCode('FL');
$address->setCity('Miramar');
$address->setCountryCode('US');
$address->setPostalCode('33027');

$eyaccessKey = 'key';
$userId = 'user id';
$password = 'password';
$xav = new \Ups\AddressValidation($accessKey, $userId, $password);
$xav->activateReturnObjectOnValidate(); //This is optional

try {
    $response = $xav->validate($address);

    echo "<pre>";
    print_r($response);
    echo "</pre>";
} catch (Exception $e) {
    var_dump($e);
}

I tried to access the data by accessing the data using json object but I am unable to access there is what I tried to do

$response->response()->AddressKeyFormat

But I was unsuccessful I am not pretty much sure how do I access this data can anyone help me out hwo do validate address .

With a nod to https://www.smarty.com/articles/ups-address-validation you do realize the agreement states this is only to be used for packages you'll ship with UPS?

There are also other issues such as it ignores/strips apartment numbers. In short, this address validator is not very good. I'd look at using something else.

Finally, if your code is as supplied SHOULD FAIL! UPS Access Keys are not "key". You have to apply and get such a key from UPS.
Example code at https://github.com/DBSInteractive/UPS-Address-Validation/blob/master/UPSSoapXAVClient.php seems to work after you get your own access key which they will eventually invalidate if you are not shipping with them.

I think your issue is you are using the wrong tool for the job. Use an address validation API that doesn't return results as SimpleXMLElements. They're annoying to work with, and there's no reason for it. What a horribly unintuitive API that is.

I have experience working with the Google Maps Geocoding API, but it's not free, although it is very inexpensive pay-as-you-go pricing that's only a few cents unless you're doing a LOT. I've used it to convert an end-user's latitude and longitude coordinates into a valid street address.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.