1

当我尝试从 API 查看错误消息时收到此错误。文档说它在 PartialError 字段中返回一个 BatchError 对象数组。当我尝试访问 BatchError 的 Index 属性时,它给了我一个错误。怎么了?

https://msdn.microsoft.com/en-us/library/bing-ads-campaign-management-addadgroups.aspx#Anchor_1

PHP Notice – yii\base\ErrorException

Trying to get property of non-object

1. in /cygdrive/c/Users/Chloe/workspace/bestsales/models/BingAds.php at line 384

      $response = $campaignProxy->GetService()->AddAdGroups($request);
    } catch (\SoapFault $e) {
      $this->handleException($e);
      return null;
    }
    $adGroupsIds = $response->AdGroupIds;
    $partialErrors = $response->PartialErrors;
    foreach ($partialErrors as $batchError) {
      Yii::error($batchError);
      $adGroup = $adGroups[$batchError->Index]; # <<<<

日志:

2016-09-11 22:15:59 [::1][1][v5adqit0fiae7bon3i1lks49m3][error][application] [
    unserialize('O:8:"stdClass":8:{s:4:"Code";i:1016;s:7:"Details";N;s:9:"ErrorCode";s:33:"CampaignServiceInvalidEntityState";s:9:"FieldPath";N;s:23:"ForwardCompatibilityMap";N;s:5:"Index";i:0;s:7:"Message";s:104:"Passed entity state is invalid. Please refer to documentation for list of valid values for given entity.";s:4:"Type";s:10:"BatchError";}'),
]
    in /cygdrive/c/Users/Chloe/workspace/bestsales/models/BingAds.php:383
    in /cygdrive/c/Users/Chloe/workspace/bestsales/models/AdGroup.php:195
    in /cygdrive/c/Users/Chloe/workspace/bestsales/models/Keyword.php:145
2016-09-11 22:15:59 [::1][1][v5adqit0fiae7bon3i1lks49m3][error][yii\base\ErrorException:8] exception 'yii\base\ErrorException' with message 'Trying to get property of non-object' in /cygdrive/c/Users/Chloe/workspace/bestsales/models/BingAds.php:384

我也试过

  Yii::error($batchError);
  Yii::error($batchError['Index']);
  Yii::error($batchError::$Index);
  Yii::error($batchError->$Index);
  Yii::error($batchError->Index);

我使用VarDumper并得到了这个:

[
    0 => stdClass#1
    (
        [Code] => 1016
        [Details] => null
        [ErrorCode] => 'CampaignServiceInvalidEntityState'
        [FieldPath] => null
        [ForwardCompatibilityMap] => null
        [Index] => 0
        [Message] => 'Passed entity state is invalid. Please refer to documentation for list of valid values for given entity.'
        [Type] => 'BatchError'
    )
]
4

1 回答 1

0

我能够修复它

$partialErrors = $response->PartialErrors;
if (!empty($partialErrors->BatchError)) $partialErrors = $partialErrors->BatchError;
foreach ($partialErrors as $batchError) {

显然foreach也能够遍历对象的属性,我确实知道它可以这样做,并且显然PartialErrors是一个具有一个名为的属性的对象BatchError然后包含一个数组(但BatchError也可能根本不存在)。这与文档不匹配,或者至少与最不意外的文档不匹配。

于 2016-09-13T08:05:14.850 回答