0

我正在使用这个包:https ://packagist.org/packages/gavroche/ups-api

当我运行这个时,我从 Laravel 异常处理程序中得到了一些奇怪的行为:

$tracking = new \UPS\Tracking(
        'KEY', 'USERNAME', 'PASS'
        );

$shipment = $tracking->track('TRACKING NUMBER HERE');

foreach($shipment->Package->Activity as $activity) {
    var_dump($activity->ActivityLocation->Address->City);
}

有趣的是,城市是 var_dumped,但 laravel 异常处理程序同时表示未定义属性:stdClass::$City。

这是 $activity 对象之一的 vardump:

object(stdClass)[518]

  public 'ActivityLocation' => 

    object(stdClass)[519]

      public 'Address' => 

        object(stdClass)[520]

          public 'City' => string 'CLEVELAND' (length=9)

          public 'StateProvinceCode' => string 'TN' (length=2)

          public 'PostalCode' => string '37311' (length=5)

          public 'CountryCode' => string 'US' (length=2)

      public 'Code' => string 'M4' (length=2)

      public 'Description' => string 'RECEPTION' (length=9)

      public 'SignedForByName' => string 'HICKMAN' (length=7)

  public 'Status' => 

    object(stdClass)[521]

      public 'StatusType' => 

        object(stdClass)[522]

          public 'Code' => string 'D' (length=1)

          public 'Description' => string 'DELIVERED' (length=9)

      public 'StatusCode' => 

        object(stdClass)[523]

          public 'Code' => string 'KB' (length=2)

  public 'Date' => string '20130426' (length=8)

  public 'Time' => string '085500' (length=6) 

$activity->ActivityLocation->Address 对象 laravel 中的任何属性都存在问题。但是,我可以请求 $activity->Status->StatusType->Description 并且没有问题。

一个常规的 for 循环解决了这个问题。似乎是增加 php 的 foreach() 的一些问题。

for ($i=0; $i < (count($shipment->Package->Activity) - 1); $i++) { 
    var_dump($shipment->Package->Activity[$i]->ActivityLocation->Address->City);
}
4

1 回答 1

0

您对此并不清楚,但您是否试图以

$activity->ActivityLocation->Address->City ?

你离开了$activity你的问题并使用了$City而不是使用City,我不确定这是否是故意的

于 2013-05-31T16:03:08.840 回答