LoadService() public method

Loads the classes within a service, so they can be used before the service is constructed.
public LoadService ( string $serviceName, string | null $version = null )
$serviceName string the service name
$version string | null the version of the service to get. If null, then the default version will be used
/**
 * Runs the example.
 * @param AdWordsUser $user the user to run the example with
 * @param string $filePath the path of the file to download the report to
 */
function DownloadCriteriaReportExample(AdWordsUser $user, $filePath)
{
    // Load the service, so that the required classes are available.
    $user->LoadService('ReportDefinitionService', ADWORDS_VERSION);
    // Create selector.
    $selector = new Selector();
    $selector->fields = array('CampaignId', 'AdGroupId', 'Id', 'Criteria', 'CriteriaType', 'Impressions', 'Clicks', 'Cost');
    // Optional: use predicate to filter out paused criteria.
    $selector->predicates[] = new Predicate('Status', 'NOT_IN', array('PAUSED'));
    // Create report definition.
    $reportDefinition = new ReportDefinition();
    $reportDefinition->selector = $selector;
    $reportDefinition->reportName = 'Criteria performance report #' . uniqid();
    $reportDefinition->dateRangeType = 'LAST_7_DAYS';
    $reportDefinition->reportType = 'CRITERIA_PERFORMANCE_REPORT';
    $reportDefinition->downloadFormat = 'CSV';
    // Exclude criteria that haven't recieved any impressions over the date range.
    $reportDefinition->includeZeroImpressions = false;
    // Set additional options.
    $options = array('version' => ADWORDS_VERSION);
    // Optional: Set skipReportHeader, skipColumnHeader, skipReportSummary to
    //     suppress headers or summary rows.
    // $options['skipReportHeader'] = true;
    // $options['skipColumnHeader'] = true;
    // $options['skipReportSummary'] = true;
    // Optional: Set includeZeroImpressions to include zero impression rows in
    //     the report output.
    // $options['includeZeroImpressions'] = true;
    // Download report.
    ReportUtils::DownloadReport($reportDefinition, $filePath, $user, $options);
    printf("Report with name '%s' was downloaded to '%s'.\n", $reportDefinition->reportName, $filePath);
}
/**
 * Runs the example.
 * @param AdWordsUser $user the user to run the example with
 * @param string $filePath the path of the file to download the report to
 */
function DownloadCriteriaReportWithAwqlExample(AdWordsUser $user, $filePath, $reportFormat)
{
    // Load the service, so that the required classes are available.
    $user->LoadService('ReportDefinitionService', 'v201206');
    // Prepare a date range for the last week. Instead you can use 'LAST_7_DAYS'.
    $dateRange = sprintf('%d,%d', date('Ymd', strtotime('-7 day')), date('Ymd', strtotime('-1 day')));
    // Create report query.
    $reportQuery = 'SELECT CampaignId, AdGroupId, Id, Criteria, CriteriaType, ' . 'Impressions, Clicks, Cost FROM CRITERIA_PERFORMANCE_REPORT ' . 'WHERE Status IN [ACTIVE, PAUSED] DURING ' . $dateRange;
    // Set additional options.
    $options = array('version' => 'v201206');
    // Download report.
    ReportUtils::DownloadReportWithAwql($reportQuery, $filePath, $user, $reportFormat, $options);
    printf("Report was downloaded to '%s'.\n", $filePath);
}
/**
 * Runs the example.
 * @param AdWordsUser $user the user to run the example with
 * @param string $filePath the path of the file to download the report to
 */
function DownloadCriteriaReportExample(AdWordsUser $user, $filePath)
{
    // Load the service, so that the required classes are available.
    $user->LoadService('ReportDefinitionService', ADWORDS_VERSION);
    // Optional: Set clientCustomerId to get reports of your child accounts
    // $user->SetClientCustomerId('INSERT_CLIENT_CUSTOMER_ID_HERE');
    // Create selector.
    $selector = new Selector();
    $selector->fields = array('CampaignId', 'AdGroupId', 'Id', 'Criteria', 'CriteriaType', 'Impressions', 'Clicks', 'Cost');
    // Optional: use predicate to filter out paused criteria.
    $selector->predicates[] = new Predicate('Status', 'NOT_IN', array('PAUSED'));
    // Create report definition.
    $reportDefinition = new ReportDefinition();
    $reportDefinition->selector = $selector;
    $reportDefinition->reportName = 'Criteria performance report #' . uniqid();
    $reportDefinition->dateRangeType = 'LAST_7_DAYS';
    $reportDefinition->reportType = 'CRITERIA_PERFORMANCE_REPORT';
    $reportDefinition->downloadFormat = 'CSV';
    // Set additional options.
    $options = array('version' => ADWORDS_VERSION);
    // Optional: Set skipReportHeader, skipColumnHeader, skipReportSummary to
    //     suppress headers or summary rows.
    // $options['skipReportHeader'] = true;
    // $options['skipColumnHeader'] = true;
    // $options['skipReportSummary'] = true;
    //
    // Optional: Set useRawEnumValues to return enum values instead of enum
    //     display values.
    // $options['useRawEnumValues'] = true;
    //
    // Optional: Set includeZeroImpressions to include zero impression rows in
    //     the report output.
    // $options['includeZeroImpressions'] = true;
    // Download report.
    $reportUtils = new ReportUtils();
    $reportUtils->DownloadReport($reportDefinition, $filePath, $user, $options);
    printf("Report with name '%s' was downloaded to '%s'.\n", $reportDefinition->reportName, $filePath);
}
/**
 * Runs the example.
 * @param AdWordsUser $user the user to run the example with
 * @param string $filePath the path of the file to download the report to
 */
function DownloadCriteriaReportExample(AdWordsUser $user, $filePath)
{
    // Load the service, so that the required classes are available.
    $user->LoadService('ReportDefinitionService', ADWORDS_VERSION);
    // Create selector.
    $selector = new Selector();
    $selector->fields = array('CampaignId', 'AdGroupId', 'Id', 'Criteria', 'CriteriaType', 'Impressions', 'Clicks', 'Cost');
    // Filter out deleted criteria.
    $selector->predicates[] = new Predicate('Status', 'NOT_IN', array('DELETED'));
    // Create report definition.
    $reportDefinition = new ReportDefinition();
    $reportDefinition->selector = $selector;
    $reportDefinition->reportName = 'Criteria performance report #' . uniqid();
    $reportDefinition->dateRangeType = 'LAST_7_DAYS';
    $reportDefinition->reportType = 'CRITERIA_PERFORMANCE_REPORT';
    $reportDefinition->downloadFormat = 'CSV';
    // Exclude criteria that haven't recieved any impressions over the date range.
    $reportDefinition->includeZeroImpressions = FALSE;
    // Set additional options.
    $options = array('version' => ADWORDS_VERSION, 'returnMoneyInMicros' => TRUE);
    // Download report.
    ReportUtils::DownloadReport($reportDefinition, $filePath, $user, $options);
    printf("Report with name '%s' was downloaded to '%s'.\n", $reportDefinition->reportName, $filePath);
}
/**
 * Runs the example.
 * @param AdWordsUser $user the user to run the example with
 */
function ParallelReportDownloadExample(AdWordsUser $user)
{
    // Load the service, so that the required classes are available.
    $user->LoadService('ReportDefinitionService', ADWORDS_VERSION);
    // Create selector.
    $selector = new Selector();
    $selector->fields = array('CampaignId', 'AdGroupId', 'Impressions', 'Clicks', 'Cost');
    // Create report definition.
    $reportDefinition = new ReportDefinition();
    $reportDefinition->selector = $selector;
    $reportDefinition->reportName = 'Custom ADGROUP_PERFORMANCE_REPORT';
    $reportDefinition->dateRangeType = 'LAST_7_DAYS';
    $reportDefinition->reportType = 'ADGROUP_PERFORMANCE_REPORT';
    $reportDefinition->downloadFormat = 'CSV';
    // Set additional options.
    $options = array('version' => ADWORDS_VERSION);
    // Optional: Set skipReportHeader, skipColumnHeader, skipReportSummary to
    //     suppress headers or summary rows.
    // $options['skipReportHeader'] = true;
    // $options['skipColumnHeader'] = true;
    // $options['skipReportSummary'] = true;
    //
    // Optional: Set useRawEnumValues to return enum values instead of enum
    //     display values.
    // $options['useRawEnumValues'] = true;
    //
    // Optional: Set includeZeroImpressions to include zero impression rows in
    //     the report output.
    // $options['includeZeroImpressions'] = true;
    $customerIds = getAllManagedCustomerIds($user);
    printf("Downloading reports for %d managed customers.\n", count($customerIds));
    $successfulReports = array();
    $failedReports = array();
    $reportDir = sys_get_temp_dir();
    $reportUtils = new ReportUtils();
    foreach ($customerIds as $customerId) {
        $filePath = sprintf('%s.csv', tempnam($reportDir, 'adgroup_'));
        $user->SetClientCustomerId($customerId);
        $retryCount = 0;
        $doContinue = true;
        do {
            $retryCount++;
            try {
                $reportUtils->DownloadReport($reportDefinition, $filePath, $user, $options);
                printf("Report for client customer ID %d successfully downloaded to: %s\n", $customerId, $filePath);
                $successfulReports[$customerId] = $filePath;
                $doContinue = false;
            } catch (ReportDownloadException $e) {
                printf("Report attempt #%d for client customer ID %d was not downloaded" . " due to: %s\n", $retryCount, $customerId, $e->getMessage());
                if ($e->GetHttpCode() >= 500 && $retryCount < MAX_RETRIES) {
                    $sleepTime = $retryCount * BACKOFF_FACTOR;
                    printf("Sleeping %d seconds before retrying report for client customer " . "ID %d.\n", $sleepTime, $customerId);
                    sleep($sleepTime);
                } else {
                    printf("Report request failed for client customer ID %d.\n", $customerId);
                    $failedReports[$customerId] = $filePath;
                    $doContinue = false;
                }
            }
        } while ($doContinue === true);
    }
    print "All downloads completed. Results:\n";
    print "Successful reports:\n";
    foreach ($successfulReports as $customerId => $filePath) {
        printf("\tClient ID %d => '%s'\n", $customerId, $filePath);
    }
    print "Failed reports:\n";
    foreach ($failedReports as $customerId => $filePath) {
        printf("\tClient ID %d => '%s'\n", $customerId, $filePath);
    }
    print "End of results.\n";
}