UploadBatchJobOperations() public method

Uploads batch job operations to the specified upload URL.
public UploadBatchJobOperations ( array $operations )
$operations array operations to be uploaded via the upload URL $uploadUrl
 /**
  * @covers BatchJobUtils::UploadBatchJobOperations
  * @expectedException BatchJobException
  */
 public function testUploadBatchJobOperationsFailsWithBatchJobException()
 {
     $curlUtils = $this->getMock('CurlUtils');
     $curlUtils->expects($this->any())->method('GetInfo')->will($this->returnValue(500));
     $curlUtils->expects($this->any())->method('Error')->will($this->returnValue('Internal server error'));
     $batchJobUtils = new BatchJobUtils(self::$DUMMY_UPLOAD_URL, 0, $curlUtils);
     $batchJobUtils->UploadBatchJobOperations(self::$DUMMY_OPERATIONS);
 }
/**
 * Runs the example.
 * @param AdWordsUser $user the user to run the example with
 */
function AddCompleteCampaignUsingBatchJobExample(AdWordsUser $user)
{
    // Get the service, which loads the required classes.
    $batchJobService = $user->GetService('BatchJobService', ADWORDS_VERSION);
    // Create a BatchJob.
    $addOp = new BatchJobOperation();
    $addOp->operator = 'ADD';
    $addOp->operand = new BatchJob();
    $addOps[] = $addOp;
    $result = $batchJobService->mutate($addOps);
    $batchJob = $result->value[0];
    // Get the upload URL from the new job.
    $uploadUrl = $batchJob->uploadUrl->url;
    printf("Created BatchJob with ID %d, status '%s' and upload 'URL' %s.\n", $batchJob->id, $batchJob->status, $uploadUrl);
    $namePrefix = uniqid();
    // Create and add an operation to create a new budget.
    $budgetOperation = buildBudgetOperation($namePrefix);
    $operations = array($budgetOperation);
    // Create and add an operation to create new campaigns.
    $campaignOperations = buildCampaignOperations($namePrefix, $budgetOperation);
    $operations = array_merge($operations, $campaignOperations);
    // Create and add operations to create new negative keyword criteria for
    // each campaign.
    $campaignCriterionOperations = buildCampaignCriterionOperations($campaignOperations);
    $operations = array_merge($operations, $campaignCriterionOperations);
    // Create and add operations to create new ad groups.
    $adGroupOperations = buildAdGroupOperations($namePrefix, $campaignOperations);
    $operations = array_merge($operations, $adGroupOperations);
    // Create and add operations to create new ad group criteria (keywords).
    $adGroupCriterionOperations = buildAdGroupCriterionOperations($adGroupOperations);
    $operations = array_merge($operations, $adGroupCriterionOperations);
    // Create and add operations to create new ad group ads (text ads).
    $adGroupAdOperations = buildAdGroupAdOperations($adGroupOperations);
    $operations = array_merge($operations, $adGroupAdOperations);
    // Use BatchJobUtils to upload all operations.
    $batchJobUtils = new BatchJobUtils($batchJob->uploadUrl->url);
    $batchJobUtils->UploadBatchJobOperations($operations);
    printf("Uploaded %d operations for batch job with ID %d.\n", count($operations), $batchJob->id);
    // Poll for completion of the batch job using an exponential back off.
    $pollAttempts = 0;
    $isPending = true;
    do {
        $sleepSeconds = POLL_FREQUENCY_SECONDS * pow(2, $pollAttempts);
        printf("Sleeping %d seconds...\n", $sleepSeconds);
        sleep($sleepSeconds);
        $selector = new Selector();
        $selector->fields = array('Id', 'Status', 'DownloadUrl', 'ProcessingErrors', 'ProgressStats');
        $selector->predicates[] = new Predicate('Id', 'EQUALS', $batchJob->id);
        $batchJob = $batchJobService->get($selector)->entries[0];
        printf("Batch job ID %d has status '%s'.\n", $batchJob->id, $batchJob->status);
        $pollAttempts++;
        if ($batchJob->status !== 'ACTIVE' && $batchJob->status !== 'AWAITING_FILE' && $batchJob->status !== 'CANCELING') {
            $isPending = false;
        }
    } while ($isPending && $pollAttempts <= MAX_POLL_ATTEMPTS);
    if ($isPending) {
        throw new BatchJobException(sprintf("Job is still pending state after polling %d times.", MAX_POLL_ATTEMPTS));
    }
    if ($batchJob->processingErrors !== null) {
        $i = 0;
        foreach ($batchJob->processingErrors as $processingError) {
            printf(" Processing error [%d]: errorType=%s, trigger=%s, errorString=%s," . " fieldPath=%s, reason=%s\n", $i++, $processingError->ApiErrorType, $processingError->trigger, $processingError->errorString, $processingError->fieldPath, $processingError->reason);
        }
    } else {
        printf("No processing errors found.\n");
    }
    if ($batchJob->downloadUrl !== null && $batchJob->downloadUrl->url !== null) {
        $xmlResponse = $batchJobUtils->DownloadBatchJobResults($batchJob->downloadUrl->url);
        printf("Downloaded results from %s:\n", $batchJob->downloadUrl->url);
        $deserializer = new XmlDeserializer(BatchJobUtils::$CLASS_MAP);
        $mutateResponse = $deserializer->ConvertXmlToObject($xmlResponse);
        if (empty($mutateResponse)) {
            printf("  No results available.\n");
        } else {
            foreach ($mutateResponse->rval as $mutateResult) {
                $outcome = $mutateResult->errorList === null ? 'SUCCESS' : 'FAILURE';
                printf("  Operation [%d] - %s\n", $mutateResult->index, $outcome);
            }
        }
    } else {
        printf("No results available for download.\n");
    }
}