src/Google/Ads/GoogleAds/vX 中的原始碼
Google Ads API PHP 用戶端程式庫目錄,其中 X 是 Google Ads API
版本) 是系統透過 GAPIC (已產生的 API 用戶端) 產生
產生器 (根據
原始版本
檔案。
接著,系統會修改產生的原始碼,讓其包含特徵和類別參照,以便使用 GoogleAdsClient 類別 (透過呼叫 GoogleAdsClientBuilder::build() 建立) 建立可與 Google Ads API 搭配使用的服務用戶端。GoogleAdsClient 和 GoogleAdsClientBuilder 都是位於 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]) ]) );