/** * Runs the example. * @param AdWordsUser $user the user to run the example with * @param string $adGroupId the ID of the ad group to hypothetically add the * text ad to */ function ValidateTextAdExample(AdWordsUser $user, $adGroupId) { // Get the service, which loads the required classes. Passing true for the // parameter $validateOnly will ensure that ads aren't created. $adGroupAdValidationService = $user->GetService('AdGroupAdService', ADWORDS_VERSION, null, null, true); // Create invalid text ad. $textAd = new TextAd(); $textAd->headline = 'This headline is too long and will cause an error'; $textAd->description1 = 'Description Line 1'; $textAd->description2 = 'Description Line 2'; $textAd->displayUrl = 'www.example.com'; $textAd->finalUrls = array('http://www.example.com'); // Create ad group ad. $adGroupAd = new AdGroupAd(); $adGroupAd->adGroupId = $adGroupId; $adGroupAd->ad = $textAd; // Create operation. $operation = new AdGroupAdOperation(); $operation->operand = $adGroupAd; $operation->operator = 'ADD'; $operations = array($operation); // Make the mutate request. try { $result = $adGroupAdValidationService->mutate($operations); printf("The text ad is valid.\n"); } catch (SoapFault $e) { $errors = ErrorUtils::GetApiErrors($e); if (sizeof($errors) > 0) { printf("The text ad is invalid for the following reasons:\n"); foreach ($errors as $error) { printf(" %s @ %s\n", $error->errorString, $error->fieldPath); } } else { // Not an API error, so throw it up a level. throw $e; } } }
/** * Runs the example. * @param AdWordsUser $user the user to run the example with * @param string $adGroupId the ID of the ad group to hypothetically add the * expanded text ad to */ function ValidateTextAdExample(AdWordsUser $user, $adGroupId) { // Get the service, which loads the required classes. Passing true for the // parameter $validateOnly will ensure that ads aren't created. $adGroupAdValidationService = $user->GetService('AdGroupAdService', ADWORDS_VERSION, null, null, true); // Create invalid expanded text ad. $expandedTextAd = new ExpandedTextAd(); $expandedTextAd->headlinePart1 = 'Luxury Cruise to Mars'; $expandedTextAd->headlinePart2 = 'Visit the Red Planet in style.'; $expandedTextAd->description = 'Low-gravity fun for all astronauts in orbit.'; $expandedTextAd->finalUrls = array('http://www.example.com'); // Create ad group ad. $adGroupAd = new AdGroupAd(); $adGroupAd->adGroupId = $adGroupId; $adGroupAd->ad = $expandedTextAd; // Create operation. $operation = new AdGroupAdOperation(); $operation->operand = $adGroupAd; $operation->operator = 'ADD'; $operations = array($operation); // Make the mutate request. try { $result = $adGroupAdValidationService->mutate($operations); printf("The expanded text ad is valid.\n"); } catch (SoapFault $e) { $errors = ErrorUtils::GetApiErrors($e); if (sizeof($errors) > 0) { printf("The expanded text ad is invalid for the following reasons:\n"); foreach ($errors as $error) { printf(" %s @ %s\n", $error->errorString, $error->fieldPath); } } else { // Not an API error, so throw it up a level. throw $e; } } }
/** * Runs the example. * @param AdWordsUser $user the user to run the example with * @param string $adGroupId the if the ad group to add the text ads to */ function HandlePolicyViolationErrorExample(AdWordsUser $user, $adGroupId) { // Get the service, which loads the required classes. $adGroupAdService = $user->GetService('AdGroupAdService', ADWORDS_VERSION); // Get validateOnly version of the AdGroupAdService. $adGroupAdValidationService = $user->GetService('AdGroupAdService', ADWORDS_VERSION, null, null, true); // Create text ad that violates an exemptable policy. This ad will only // trigger an error in the production environment. $textAd = new TextAd(); $textAd->headline = 'Mars Cruise !!!'; $textAd->description1 = 'Visit the Red Planet in style.'; $textAd->description2 = 'Low-gravity fun for everyone!'; $textAd->displayUrl = 'www.example.com'; $textAd->finalUrls = array('http://www.example.com/'); // Create ad group ad. $adGroupAd = new AdGroupAd(); $adGroupAd->adGroupId = $adGroupId; $adGroupAd->ad = $textAd; // Create operation. $operation = new AdGroupAdOperation(); $operation->operand = $adGroupAd; $operation->operator = 'ADD'; $operations = array($operation); try { // Make the mutate request. $result = $adGroupAdValidationService->mutate($operations); } catch (SoapFault $fault) { $errors = ErrorUtils::GetApiErrors($fault); if (sizeof($errors) == 0) { // Not an API error, so throw fault. throw $fault; } $operationIndicesToRemove = array(); foreach ($errors as $error) { if ($error->ApiErrorType == 'PolicyViolationError') { $operationIndex = ErrorUtils::GetSourceOperationIndex($error); $operation = $operations[$operationIndex]; printf("Ad with headline '%s' violated %s policy '%s'.\n", $operation->operand->ad->headline, $error->isExemptable ? 'exemptable' : 'non-exemptable', $error->externalPolicyName); if ($error->isExemptable) { // Add exemption request to the operation. printf("Adding exemption request for policy name '%s' on text " . "'%s'.\n", $error->key->policyName, $error->key->violatingText); $operation->exemptionRequests[] = new ExemptionRequest($error->key); } else { // Remove non-exemptable operation. print "Removing the operation from the request.\n"; $operationIndicesToRemove[] = $operationIndex; } } else { // Non-policy error returned, throw fault. throw $fault; } } $operationIndicesToRemove = array_unique($operationIndicesToRemove); rsort($operationIndicesToRemove, SORT_NUMERIC); if (sizeof($operationIndicesToRemove) > 0) { foreach ($operationIndicesToRemove as $operationIndex) { unset($operations[$operationIndex]); } } } if (sizeof($operations) > 0) { // Retry the mutate request. $result = $adGroupAdService->mutate($operations); // Display results. foreach ($result->value as $adGroupAd) { printf("Text ad with headline '%s' and ID '%s' was added.\n", $adGroupAd->ad->headline, $adGroupAd->ad->id); } } else { print "All the operations were invalid with non-exemptable errors.\n"; } }
/** * Test getting the API errors from a SOAP fault. * @param SoapFault $fault the SOAP fault to get errors from * @param array $expected the expected errors * @covers ErrorUtils::GetApiErrors * @dataProvider SoapFaultProvider */ public function testGetApiErrors(SoapFault $fault, array $expected) { $errors = ErrorUtils::GetApiErrors($fault); $this->assertEquals($expected, $errors); }