Google Ads API PHP istemci kitaplığının src/Google/Ads/GoogleAds/vX
dizininde bulunan kaynak kod (X, Google Ads API sürümüdür), yayınlanan proto dosyalarına göre GAPIC (Oluşturulan API İstemcisi) Oluşturucu kullanılarak otomatik olarak oluşturulur.
Oluşturulan kaynak kod daha sonra, GoogleAdsClientBuilder::build()
çağrısı yapılarak oluşturulan GoogleAdsClient
sınıfını kullanarak Google Ads API ile çalışan hizmet istemcileri oluşturmak için gereken özelliklere ve sınıflara referanslar içerecek şekilde değiştirilir. Hem GoogleAdsClient
hem de GoogleAdsClientBuilder
, src/Google/Ads/GoogleAds/Lib/vX/
'de bulunan manuel olarak oluşturulmuş sınıflardır.
Oluşturulan sınıf konumları
Son işleme tabi tutulan hizmet istemcileri src/Google/Ads/GoogleAds/VX/Services/Client/
içinde bulunur.
Kullanım
Bir istek nesnesi oluşturmanız ve bunu kullanmak istediğiniz müşteriye iletmeniz gerekir.
Bazı istemcilerde zorunlu parametreleri iletmek için build()
adlı kullanışlı bir yöntem de bulunduğundan, bazı durumlarda istek nesnesi oluşturmanın birden fazla yolu vardır.
Örnek 1.1: Zorunlu parametreler içeren yöntemler
Aşağıdaki örnek kodda, CampaignService::mutate()
işlevinin nasıl çağrılacağı gösterilmektedir. Tüm parametreler ($customerId
ve $operations
) zorunlu parametre olduğundan kodda her iki parametreyi de kabul eden build()
oluşturulur.
Desen 1
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient(); $response = $campaignServiceClient->mutateCampaigns( MutateCampaignsRequest::build( $customerId, $campaignOperations ) );
Desen 2
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient(); $request = (new MutateCampaignsRequest()) ->setCustomerId($customerId) ->setCampaignOperations($campaignOperations); $response = $campaignServiceClient->mutateCampaigns($request);
1.2 numaralı örnek: Zorunlu ve isteğe bağlı parametreler içeren yöntemler
Aşağıdaki örnek kodda GoogleAdsServiceClient::search()
çağrılıyor. Bu örnekte, kodda oluşturulan build()
, zorunlu parametreler olduğu için yalnızca iki parametreyi ($customerId
ve $query
) kabul eder. LIMIT
yan tümcesi dikkate alınmadan sorguyla eşleşen sonuçların toplam sayısını istemek için setReturnTotalResultsCount()
kullanarak açıkça ayarlamanız gerekir. Alternatif olarak, 3. kalıpta gösterildiği gibi tüm parametreleri birlikte SearchGoogleAdsRequest
sınıfının kurucusuna iletebilirsiniz.
Desen 1
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); $response = $googleAdsServiceClient->search( SearchGoogleAdsRequest::build($customerId, $query) ->setReturnTotalResultsCount(true) );
Desen 2
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); $request = (new SearchGoogleAdsRequest()) ->setCustomerId($customerId) ->setQuery($query) ->setReturnTotalResultsCount(true); $response = $googleAdsServiceClient->search($request);
Desen 3
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); $request = (new SearchGoogleAdsRequest([ 'customer_id' => $customerId, 'query' => $query, 'return_total_results_count' => true ]); $response = $googleAdsServiceClient->search($request);
2. örnek: Yalnızca isteğe bağlı parametreler içeren yöntemler
Bu örnekte, GeoTargetConstantServiceClient::suggestGeoTargetConstants()
işlevinin nasıl çağrılacağı gösterilmektedir. GeoTargetConstantServiceClient::suggestGeoTargetConstants()
parametrelerinin tümü isteğe bağlı olduğundan bu durumda build()
kaynak kodda oluşturulmaz. İstekte bulunma nesnesini kendiniz oluşturmanız gerekir.
Desen 1
$geoTargetConstantServiceClient = $googleAdsClient->getGeoTargetConstantServiceClient(); $request = (new SuggestGeoTargetConstantsRequest()) ->setLocale($locale) ->setCountryCode($countryCode) ->setLocationNames(new LocationNames(['names' => $locationNames])); $response = $geoTargetConstantServiceClient->suggestGeoTargetConstants($request);
Desen 2
$geoTargetConstantServiceClient = $googleAdsClient->getGeoTargetConstantServiceClient(); $response = $geoTargetConstantServiceClient->suggestGeoTargetConstants( new SuggestGeoTargetConstantsRequest([ 'locale' => $locale, 'country_code' => $countryCode, 'location_names' => new LocationNames(['names' => $locationNames]) ]) );