Google Ads API PHP istemci kitaplığının src/Google/Ads/GoogleAds/VX dizinindeki kaynak kodu (X, Google Ads API sürüm numarası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 kodu daha sonra, GoogleAdsClientBuilder::build() çağrılarak oluşturulan GoogleAdsClient sınıfını kullanarak Google Ads API ile çalışan hizmet istemcilerini 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/ içinde bulunan ve manuel olarak yönetilen sınıflardır.
Oluşturulan sınıf konumları
İşlem sonrası hizmet istemcileri src/Google/Ads/GoogleAds/VX/Services/Client/ (örneğin, V26) içinde, ilgili istek ve yanıt mesajı sınıfları (ör. MutateCampaignsRequest ve SearchGoogleAdsRequest) ise src/Google/Ads/GoogleAds/VX/Services/ içinde bulunur. Alt API sürümleri (ör. v25.1), ana sürüm dizininde (V25) oluşturulur.
Kullanım
Bir istek nesnesi oluşturmanız ve bunu kullanmak istediğiniz hizmet istemcisine iletmeniz gerekir. Parametrelerin zorunlu veya isteğe bağlı olmasına bağlı olarak, zorunlu parametreleri iletmek için statik bir build() fabrika yöntemi de dahil olmak üzere istek nesnesi oluşturmanın birden fazla yolu vardır.
Örnek 1.1: Zorunlu parametreleri olan yöntemler
Aşağıdaki örnek kodda CampaignServiceClient::mutateCampaigns() yönteminin nasıl çağrılacağı gösterilmektedir. Hem $customerId hem de $campaignOperations zorunlu parametreler olduğundan, istek sınıfında her iki parametreyi de kabul eden statik bir build() fabrika yöntemi oluşturulur.
1. model: Statik derleme yöntemi (önerilir)
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns(
MutateCampaignsRequest::build(
$customerId,
$campaignOperations
)
);
2. kalıp: Akıcı ayarlayıcılar
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$request = (new MutateCampaignsRequest())
->setCustomerId($customerId)
->setCampaignOperations($campaignOperations);
$response = $campaignServiceClient->mutateCampaigns($request);
Örnek 1.2: Gerekli ve isteğe bağlı parametreleri olan yöntemler
Aşağıdaki örnek kod GoogleAdsServiceClient::search() yöntemini çağırır. Bu örnekte, oluşturulan build() yöntemi yalnızca iki parametre ($customerId ve $query) kabul eder çünkü bunlar zorunlu parametrelerdir. LIMIT ifadesini yok sayarak sorguyla eşleşen toplam sonuç sayısını istemek için setReturnTotalResultsCount() kullanarak bunu açıkça ayarlayabilirsiniz. Alternatif olarak, tüm parametreleri birlikte ilişkisel bir dizide SearchGoogleAdsRequest oluşturucusuna iletebilirsiniz (Desen 3'te gösterildiği gibi).
1. kalıp: Akıcı ayarlayıcılar içeren statik derleme yöntemi
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
$response = $googleAdsServiceClient->search(
SearchGoogleAdsRequest::build($customerId, $query)
->setReturnTotalResultsCount(true)
);
2. kalıp: Akıcı ayarlayıcılar
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
$request = (new SearchGoogleAdsRequest())
->setCustomerId($customerId)
->setQuery($query)
->setReturnTotalResultsCount(true);
$response = $googleAdsServiceClient->search($request);
3. kalıp: Dizi oluşturucu
$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() yönteminin nasıl çağrılacağı gösterilmektedir. GeoTargetConstantServiceClient::suggestGeoTargetConstants() parametrelerinin tümü isteğe bağlı olduğundan bu durumda kaynak kodunda build() oluşturulmaz. İstek nesnesini ayarlayıcıları veya dizi oluşturucuyu kullanarak oluşturmanız gerekir.
1. kalıp: Akıcı ayarlayıcılar (önerilir)
$geoTargetConstantServiceClient =
$googleAdsClient->getGeoTargetConstantServiceClient();
$request = (new SuggestGeoTargetConstantsRequest())
->setLocale($locale)
->setCountryCode($countryCode)
->setLocationNames(new LocationNames(['names' => $locationNames]));
$response =
$geoTargetConstantServiceClient->suggestGeoTargetConstants($request);
2. kalıp: Dizi oluşturucu
$geoTargetConstantServiceClient =
$googleAdsClient->getGeoTargetConstantServiceClient();
$response = $geoTargetConstantServiceClient->suggestGeoTargetConstants(
new SuggestGeoTargetConstantsRequest([
'locale' => $locale,
'country_code' => $countryCode,
'location_names' => new LocationNames(['names' => $locationNames])
])
);