Я решил эту проблему.На самом деле, я использовал один и тот же экземпляр службы для каждой итерации, которая была неправильной.
$ adGroupService = $ adWordsServices-> get ($ session, AdGroupService :: class);
$fields =['Id','Name','Status','ServingStatus','AdvertisingChannelType','AdvertisingChannelSubType'];
$campaignService = $adWordsServices->get($session, CampaignService::class);
$adGroupId = 0;
for ($i=0; $i <count($campaignNameArr) ; $i++) {
$operations =[];
$adGroupService = $adWordsServices->get($session, AdGroupService::class); // HERE IS THE LINE OF CODE.
$_collection = self::fetchCampaigns(
$request,
$campaignService,
$fields,
$entriesPerPage,
$pageNo,
$campaignNameArr[$i]
);
if(count($_collection) >0)
{
$flag= true;
foreach ($_collection as $key => $campaign) {
$campaignId = $campaign->getID();
}
}
else
{
echo "No Campaign with name of :". $campaignNameArr[$i];
echo "<br/>";
continue;
}
$msg = 'Created';
echo $campaignId."<br/>";
// Create an ad group with required and optional settings.
$adGroup = new AdGroup();
$adGroup->setCampaignId($campaignId);
// $adGroup->setName($productTitle.' #' . uniqid());
$adGroup->setName($productTitle);
// Set bids (required).
$bid = new CpcBid();
$money = new Money();
$money->setMicroAmount(10000);
$bid->setBid($money);
$biddingStrategyConfiguration = new BiddingStrategyConfiguration();
$biddingStrategyConfiguration->setBids([$bid]);
$adGroup->setBiddingStrategyConfiguration($biddingStrategyConfiguration);
// Set additional settings (optional).
$adGroup->setStatus(AdGroupStatus::ENABLED);
// Targeting restriction settings. Depending on the criterionTypeGroup
// value, most TargetingSettingDetail only affect Display campaigns.
// However, the USER_INTEREST_AND_LIST value works for RLSA campaigns -
// Search campaigns targeting using a remarketing list.
$targetingSetting = new TargetingSetting();
$details = [];
// Restricting to serve ads that match your ad group placements.
// This is equivalent to choosing "Target and bid" in the UI.
$details[] = new TargetingSettingDetail(CriterionTypeGroup::PLACEMENT, false);
// Using your ad group verticals only for bidding. This is equivalent
// to choosing "Bid only" in the UI.
$details[] = new TargetingSettingDetail(CriterionTypeGroup::VERTICAL, true);
$targetingSetting->setDetails($details);
$adGroup->setSettings([$targetingSetting]);
// Set the rotation mode.
$rotationMode = new AdGroupAdRotationMode(AdRotationMode::OPTIMIZE);
$adGroup->setAdGroupAdRotationMode($rotationMode);
// Create an ad group operation and add it to the operations list.
$operation = new AdGroupOperation();
$operation->setOperand($adGroup);
$operation->setOperator(Operator::ADD);
$operations[] = $operation;
// Create the ad groups on the server and print out some information for
// each created ad group.
$result = $adGroupService->mutate($operations);
$adGroupId = $adGroup->getId();
foreach ($result->getValue() as $adGroup) {
printf(
"Ad group with name '%s' and ID %d was added.\n",
$adGroup->getName(),
$adGroup->getId()
);
}
}