Hi. Please, help me.
I'm using a check. As written in the documentation.
$session->setValidateOnly(true);
$adWordsServices = new AdWordsServices();
$adGroupAdService = $adWordsServices->get($session, AdGroupAdService::class);
$operations = [];
$expandedTextAd = new ExpandedTextAd();
$expandedTextAd->setHeadlinePart1($HeadlinePart1);
$expandedTextAd->setHeadlinePart2($HeadlinePart2);
$expandedTextAd->setDescription($sourceAdText);
$expandedTextAd->setFinalUrls([$sourceAdHref]);
$adGroupAd = new AdGroupAd();
$adGroupAd->setAdGroupId($adsGroupsSync[$sourceAdGroupId]);
$adGroupAd->setAd($expandedTextAd);
$adGroupAd->setStatus(AdGroupAdStatus::PAUSED);
$operation = new AdGroupAdOperation();
operation->setOperand($adGroupAd);
$operation->setOperator(Operator::ADD);
$operations[] = $operation;
try {
// Try creating an ad group ad on the server.
$result = $adGroupAdService->mutate($operations);
} catch (ApiException $apiException) {
$operationIndicesToRemove = [];
foreach ($apiException->getErrors() as $error) {
// Get the index of the failed operation from the error's field path
// elements.
$fieldPathElements = $error->getFieldPathElements();
$firstFieldPathElement = null;
if ($fieldPathElements !== null && count($fieldPathElements) > 0) {
$firstFieldPathElement = $fieldPathElements[0];
}
if ($firstFieldPathElement === null
|| $firstFieldPathElement->getField() !== 'operations'
|| $firstFieldPathElement->getIndex() === null) {
// If the operation index is not present on the first error field
// path element, then there's no way to determine which operation to
// remove, so simply throw the exception.
throw $apiException;
}
$operationIndex = $firstFieldPathElement->getIndex();
$operation = $operations[$operationIndex];
if ($error instanceof PolicyViolationError) {
if ($error->getIsExemptable() === true) {
$operation->setExemptionRequests(
[new ExemptionRequest($error->getKey())]);
} else {
$operationIndicesToRemove[] = $operationIndex;
}
} else {
$operationIndicesToRemove[] = $operationIndex;
}
}
$operationIndicesToRemove = array_unique($operationIndicesToRemove);
rsort($operationIndicesToRemove, SORT_NUMERIC);
foreach ($operationIndicesToRemove as $operationIndex) {
unset($operations[$operationIndex]);
}
}
if (count($operations) > 0) {
// Make the mutate request to really add an ad group ad.
$session->setValidateOnly(false);
try {
$result = $adGroupAdService->mutate($operations);
} catch (ApiException $e) {
$errors = $e->getErrors();
if (count($errors) > 0) {
foreach ($errors as $error) {
// todo
}
}
}
}