0

I need to do some basic Fedex package tracking. I'm using Jeremy Dunn's php-fedex-api-wrapper. My FedEx developer credentials are setup and I am able to authenticate via the FedEx api.

The problem: when I run a basic track request using the api-wrapper code I get back a result that I am unsure how to interpret. It looks like a structure that I should have no trouble parsing but I cannot.

Here are the first few lines of the result code that I see when I var_dump($trackReply)

  object(FedEx\TrackService\ComplexType\TrackReply)#66 (2) {
  ["name":protected]=>
  string(10) "TrackReply"
  ["values":protected]=>
  array(4) {

If I cast the result as an array and print_r it looks like this (truncated version):

    Array
    (
        [0] => FedEx\TrackService\ComplexType\TrackReply Object
            (
                [name:protected] => TrackReply
                [values:protected] => Array
                    (
                        [HighestSeverity] => SUCCESS
                        [Notifications] => Array
                            (
                                [0] => FedEx\TrackService\ComplexType\Notification Object
                                    (
                                        [name:protected] => Notification
                                        [values:protected] => Array
                                            (
                                                [Severity] => SUCCESS
                                                [Source] => trck
                                                [Code] => 0
                                                [Message] => Request was successfully processed.
                                                [LocalizedMessage] => Request was successfully

 processed.
                                        )

                                )

                        )

The $trackResult is obviously an object, but I cannot figure out how to access its specific properties. When it is cast as an array, element zero is clearly an object, so I would think I could access the object properties with

I tried $test = $trackResult[0];// which should now be an object, right? and then

I looked at this StackOverflow post and it seems to be relevant: Convert a PHP object to an associative array

So I tried one of the solutions from the link above: $array = json_decode(json_encode($nested_object), true); But, when I print_r($array) I get essentially an empty array like this:

Array
(
)
Jack Albright
  • 509
  • 3
  • 11
  • 1
    If [this source](http://www.flipagro.com/css/library/php-fedex-api-wrapper-master/doc/class-FedEx.TrackService.ComplexType.TrackReply.html) can be trusted, the object should have a `toArray` method. – El_Vanja Nov 17 '20 at 22:25
  • 1
    How are you "casting" it to an array? It looks like you're creating a new array with the object as the first entry instead. – rickdenhaan Nov 17 '20 at 22:27
  • El_Vanja, your suggestion did the trick! thanks. If you write it as an answer I'll give you credit for the solution. I was attempting to cast the object as an array incorrectly by writing (array)$trackReply instead of $trackReply->toArray(); – Jack Albright Nov 18 '20 at 14:25

0 Answers0