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])
    ])
);