1

I get this error when I try to view the error messages from the API. The documentation says it returns an array of BatchError objects in the PartialError field. When I try to access the Index property of BatchError, it gives me an error. What is wrong?

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]; # <<<<

Logs:

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

I also tried

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

I used VarDumper and got this:

[
    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'
    )
]
Chloe
  • 25,162
  • 40
  • 190
  • 357
  • What is `$adGroups`? As far as I can tell, there's no problem accessing `$batchError->Index` – Phil Sep 13 '16 at 06:18

1 Answers1

0

I was able to fix it with

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

Apparently foreach is able to loop over the properties of an object too, which I did know know it could do, and apparently PartialErrors is an object with one property named BatchError which then contains an array (but BatchError may also not exist at all). This doesn't match the documentation, or at least not the documentation of least surprise.

Chloe
  • 25,162
  • 40
  • 190
  • 357