我正在使用这个包: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);
}