GAPIC

Google Ads API PHP 用戶端程式庫 src/Google/Ads/GoogleAds/vX 目錄中的原始碼 (X 是 Google Ads API 版本),會根據已發布的 proto 檔案,使用 GAPIC (產生的 API 用戶端) 產生器自動產生。

接著,系統會修改產生的原始碼,讓其包含特徵和類別參照,以便使用 GoogleAdsClient 類別 (透過呼叫 GoogleAdsClientBuilder::build() 建立) 建立可與 Google Ads API 搭配使用的服務用戶端。GoogleAdsClientGoogleAdsClientBuilder 都是位於 src/Google/Ads/GoogleAds/Lib/vX/ 中手動建立的類別。

產生的類別位置

經後製的服務用戶端位於 src/Google/Ads/GoogleAds/VX/Services/Client/ 中。

用量

您必須建立要求物件,並將其傳遞至要使用的用戶端。在某些情況下,您可以透過多種方式建立要求物件,因為某些用戶端也有方便的方法 (名為 build()),可用於傳遞必要參數。

範例 1.1:含有必要參數的方法

以下程式碼範例說明如何呼叫 CampaignService::mutate()。所有參數 ($customerId$operations) 都是必要參數,因此程式碼會產生可接受這兩個參數的 build()

模式 1

$campaignServiceClient
    = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns(
    MutateCampaignsRequest::build(
      $customerId,
      $campaignOperations
    )
);

模式 2

$campaignServiceClient
    = $googleAdsClient->getCampaignServiceClient();
$request = (new MutateCampaignsRequest())
    ->setCustomerId($customerId)
    ->setCampaignOperations($campaignOperations);
$response = $campaignServiceClient->mutateCampaigns($request);

範例 1.2:含有必要參數和選用參數的方法

下列程式碼範例會呼叫 GoogleAdsServiceClient::search()。在這個範例中,程式碼中產生的 build() 只會接受兩個參數 ($customerId$query),因為這是必要參數。如要要求與查詢相符的結果總數 (不考慮 LIMIT 子句),您必須使用 setReturnTotalResultsCount() 明確設定。或者,您也可以將所有參數一起傳遞至 SearchGoogleAdsRequest 的建構函式,如模式 3 所示。

模式 1

$googleAdsServiceClient
    = $googleAdsClient->getGoogleAdsServiceClient();
$response = $googleAdsServiceClient->search(
    SearchGoogleAdsRequest::build($customerId, $query)
        ->setReturnTotalResultsCount(true)
);

模式 2

$googleAdsServiceClient
    = $googleAdsClient->getGoogleAdsServiceClient();
$request = (new SearchGoogleAdsRequest())
    ->setCustomerId($customerId)
    ->setQuery($query)
    ->setReturnTotalResultsCount(true);
$response = $googleAdsServiceClient->search($request);

模式 3

$googleAdsServiceClient
    = $googleAdsClient->getGoogleAdsServiceClient();
$request = (new SearchGoogleAdsRequest([
    'customer_id' => $customerId,
    'query' => $query,
    'return_total_results_count' => true
]);
$response = $googleAdsServiceClient->search($request);

範例 2:僅含選用參數的方法

這個範例說明如何呼叫 GeoTargetConstantServiceClient::suggestGeoTargetConstants()。由於 GeoTargetConstantServiceClient::suggestGeoTargetConstants() 的所有參數皆為選用項,因此在這種情況下,系統不會在原始碼中產生 build(),您必須自行建立要求物件。

模式 1

$geoTargetConstantServiceClient =
    $googleAdsClient->getGeoTargetConstantServiceClient();
$request = (new SuggestGeoTargetConstantsRequest())
    ->setLocale($locale)
    ->setCountryCode($countryCode)
    ->setLocationNames(new LocationNames(['names' => $locationNames]));
$response =
    $geoTargetConstantServiceClient->suggestGeoTargetConstants($request);

模式 2

$geoTargetConstantServiceClient =
    $googleAdsClient->getGeoTargetConstantServiceClient();
$response = $geoTargetConstantServiceClient->suggestGeoTargetConstants(
    new SuggestGeoTargetConstantsRequest([
        'locale' => $locale,
        'country_code' => $countryCode,
        'location_names' => new LocationNames(['names' => $locationNames])
    ])
);