0

i am working on a UPS tracking API, i have called the API to track the package, i am getting the right response, but in array format, i am new to json decoding, please tell me how to parse this in PHP, do i have to create multiple objects in PHP? i am attaching the output as well as the PHP code.

PHP Code

if (isset($_POST['af0'])) {
    if (preg_match('/^[a-z\d_]{4,80}$/i', $_POST['trackingNumber'])) {
        $cleanTrackingNumber = $_POST['trackingNumber'];
        $someArray = upsTrack("$cleanTrackingNumber");
        echo '<pre>'; print_r($someArray); echo '</pre>';

    } else {
        echo 'Invalid tracking number... sigh...';
    }
}

Output

Array
(
    [TRACKRESPONSE] => Array
        (
            [RESPONSE] => Array
                (
                    [TRANSACTIONREFERENCE] => Array
                        (
                            [XPCIVERSION] => 1.0
                        )

                    [RESPONSESTATUSCODE] => 1
                    [RESPONSESTATUSDESCRIPTION] => Success
                )

            [SHIPMENT] => Array
                (
                    [SHIPPER] => Array
                        (
                            [SHIPPERNUMBER] => A6161A
                            [ADDRESS] => Array
                                (
                                    [ADDRESSLINE1] => 132 E 43RD ST
                                    [CITY] => NEW YORK
                                    [STATEPROVINCECODE] => NY
                                    [POSTALCODE] => 10017   4019
                                    [COUNTRYCODE] => US
                                )

                        )

                    [SHIPTO] => Array
                        (
                            [ADDRESS] => Array
                                (
                                    [CITY] => TORONTO
                                    [STATEPROVINCECODE] => ON
                                    [POSTALCODE] => M5V3X1
                                    [COUNTRYCODE] => CA
                                )

                        )

                    [SHIPMENTWEIGHT] => Array
                        (
                            [UNITOFMEASUREMENT] => Array
                                (
                                    [CODE] => LBS
                                )

                            [WEIGHT] => 3.20
                        )

                    [SERVICE] => Array
                        (
                            [CODE] => 011
                            [DESCRIPTION] => STANDARD
                        )

                    [REFERENCENUMBER] => Array
                        (
                            [CODE] => 13
                            [VALUE] => A6161AD9HPK
                        )

                    [SHIPMENTIDENTIFICATIONNUMBER] => 1ZA6161A6832763249
                    [PICKUPDATE] => 20140519
                    [SCHEDULEDDELIVERYDATE] => 20140521
                    [PACKAGE] => Array
                        (
                            [TRACKINGNUMBER] => 1ZA6161A6832763249
                            [ACTIVITY] => Array
                                (
                                    [ACTIVITYLOCATION] => Array
                                        (
                                            [ADDRESS] => Array
                                                (
                                                    [CITY] => SECAUCUS
                                                    [STATEPROVINCECODE] => NJ
                                                    [COUNTRYCODE] => US
                                                )

                                        )

                                    [STATUS] => Array
                                        (
                                            [STATUSTYPE] => Array
                                                (
                                                    [CODE] => I
                                                    [DESCRIPTION] => DEPARTURE SCAN
                                                )

                                            [STATUSCODE] => Array
                                                (
                                                    [CODE] => DP
                                                )

                                        )

                                    [DATE] => 20140520
                                    [TIME] => 053000
                                )

                            [MESSAGE] => Array
                                (
                                    [CODE] => 01
                                    [DESCRIPTION] => On Time
                                )

                            [PACKAGEWEIGHT] => Array
                                (
                                    [UNITOFMEASUREMENT] => Array
                                        (
                                            [CODE] => LBS
                                        )

                                    [WEIGHT] => 3.20
                                )

                            [REFERENCENUMBER] => Array
                                (
                                    [CODE] => 19
                                    [VALUE] => MMTD71EUY061A
                                )

                        )

                )

        )

)

1 Answer 1

1

If you already have the output in Array format then you don't have anything more to do with json decoding.

You can use $someArray to get any data from the array. It's up to you if you want to just display it or save it, for example in a database.

What is it you trying to accomplish?

Sign up to request clarification or add additional context in comments.

1 Comment

echo $someArray['TRACKRESPONSE']['SHIPMENT']['SHIPPER']['SHIPPERNUMBER']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.