GAPIC

Google Ads API PHP istemci kitaplığının src/Google/Ads/GoogleAds/vX dizininde yer alan kaynak kodu (X, Google Ads API sürümüdür), yayınlanan protokol dosyalarına dayanarak GAPIC (Oluşturulan API İstemcisi) Oluşturucu kullanılarak otomatik olarak oluşturulur.

Oluşturulan kaynak kodu, daha sonra GoogleAdsClientBuilder::build() çağrısı yapılarak oluşturulan GoogleAdsClient sınıfını kullanarak Google Ads API ile çalışan hizmet istemcilerini oluşturmak için gereken özellik ve sınıf referanslarını içerecek şekilde değiştirilir. Hem GoogleAdsClient hem de GoogleAdsClientBuilder, src/Google/Ads/GoogleAds/Lib/vX/ konumunda bulunan manuel olarak oluşturulmuş sınıflardır.

GAPIC v2 kaynak kodu

v20.1.0 sürümünden itibaren, istemci kitaplığı src/Google/Ads/GoogleAds/vX içinde GAPIC kaynak kodunun yeni bir sürümünü de içermektedir. Bu sürüm, hizmet istemcilerinin yöntemlerine bir istek nesnesinin iletilmesini destekler. GAPIC v2 adlı bu yeni sürüm, gelecekteki yeni özellikler için hazırlık görevi görür. Önceki GAPIC kaynak kodu olan GAPIC v1, oluşturulmaya devam eder ve 2023'ün sonuna kadar her sürüme dahil edilir.

Google Ads API PHP istemci kitaplığı, useGapicV2Source yapılandırma ayarını kullanarak GoogleAdsClient uygulamasına bağlanacak sürümü seçmenize olanak tanır. Bu ayar true olarak ayarlandığında, istemci kitaplığı GAPIC v2 hizmet istemcileri oluşturan bir GoogleAdsClient nesnesi oluşturur.

Oluşturulan sınıf konumları

İki sınıf türünün GAPIC sürümleri arasındaki dosya konumu farklılıkları aşağıda açıklanmıştır:

GAPIC tarafından oluşturulan istemciler ve ilgili dosyalar GAPIC v1
src/Google/Ads/GoogleAds/VX/Services/Gapic/
GAPIC v2: Yok. Yalnızca işleme sonrası bir dosya oluşturulur.
İşleme sonrası istemciler GAPIC v1
src/Google/Ads/GoogleAds/VX/Services/
GAPIC v2
src/Google/Ads/GoogleAds/VX/Services/Client/

Kullanım

GAPIC v1, her istek parametresini doğrudan bir yönteme iletmenizi; GAPIC v2 ise bunun yerine bir istek nesnesi iletmenizi gerektirir. GAPIC v2 aynı zamanda gerekli parametreleri geçirmek üzere build() adlı uygun bir yöntem de oluşturduğundan, bazı durumlarda istek nesnesi oluşturmanın birden fazla yolunun olduğunu unutmayın.

Örnek 1.1: Gerekli parametrelere sahip yöntemler

Aşağıdaki örnek kod, GAPIC v1 ve v2'de CampaignService::mutate() çağrısını karşılaştırır. Tüm parametrelerin ($customerId ve $operations) gerekli parametreler olduğunu, bu nedenle her iki parametreyi de kabul eden build() değerinin GAPIC v2 kodunda oluşturulduğunu unutmayın.

Kalıp 1 GAPIC v1
$campaignServiceClient
    = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns(
    $customerId,
    $campaignOperations
);
      
GAPIC v2
$campaignServiceClient
    = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns(
    MutateCampaignsRequest::build(
      $customerId,
      $campaignOperations
    )
);
      
Kalıp 2 GAPIC v1
N/A
GAPIC v2
$campaignServiceClient
    = $googleAdsClient->getCampaignServiceClient();
$request = (new MutateCampaignsRequest())
    ->setCustomerId($customerId)
    ->setCampaignOperations($campaignOperations);
$response = $campaignServiceClient->mutateCampaigns($request);
      

Örnek 1.2: Gerekli parametreler ve isteğe bağlı parametreler içeren yöntemler

Aşağıdaki örnek kod, GAPIC v1 ve v2'de GoogleAdsServiceClient::search() çağrısını karşılaştırır. Bu örnekte, GAPIC v2 kaynak kodunda oluşturulan build(), zorunlu parametreler oldukları için yalnızca iki parametreyi ($customerId ve $query) kabul eder. İsteğe bağlı bir parametre olan sayfa boyutu ayarlamak için setPageSize() parametresini kullanarak bunu açık bir şekilde ayarlamanız gerekir. Alternatif olarak, kalıp 3'te gösterildiği gibi tüm parametreleri SearchGoogleAdsRequest oluşturucuya birlikte geçirebilirsiniz.

Kalıp 1 GAPIC v1
$googleAdsServiceClient
    = $googleAdsClient->getGoogleAdsServiceClient();
$response = $googleAdsServiceClient->search(
    $customerId,
    $query,
    ['pageSize' => self::PAGE_SIZE]
);
      
GAPIC v2
$googleAdsServiceClient
    = $googleAdsClient->getGoogleAdsServiceClient();
$response = $googleAdsServiceClient->search(
    SearchGoogleAdsRequest::build($customerId, $query)
        ->setPageSize(self::PAGE_SIZE)
);
      
Kalıp 2 GAPIC v1
N/A
GAPIC v2
$googleAdsServiceClient
    = $googleAdsClient->getGoogleAdsServiceClient();
$request = (new SearchGoogleAdsRequest())
    ->setCustomerId($customerId)
    ->setQuery($query)
    ->setPageSize(self::PAGE_SIZE);
$response = $googleAdsServiceClient->search($request);
      
Kalıp 3 GAPIC v1
N/A
GAPIC v2
$googleAdsServiceClient
    = $googleAdsClient->getGoogleAdsServiceClient();
$request = (new SearchGoogleAdsRequest([
    'customer_id' => $customerId,
    'query' => $query,
    'page_size' => self::PAGE_SIZE
]);
$response = $googleAdsServiceClient->search($request);
      

2. Örnek: Yalnızca isteğe bağlı parametreler içeren yöntemler

GAPIC v1 ve v2'de GeoTargetConstantServiceClient::suggestGeoTargetConstants() çağrısını karşılaştırın. Tüm GeoTargetConstantServiceClient::suggestGeoTargetConstants() parametreleri isteğe bağlı olduğundan, bu durumda GAPIC v2 kaynak kodunda build() oluşturulmaz. İstek nesnesini kendiniz oluşturmanız gerekir.

Kalıp 1 GAPIC v1
$geoTargetConstantServiceClient =
    $googleAdsClient->getGeoTargetConstantServiceClient();
$response = $geoTargetConstantServiceClient->suggestGeoTargetConstants([
    'locale' => $locale,
    'countryCode' => $countryCode,
    'locationNames' => new LocationNames(['names' => $locationNames])
]);
      
GAPIC v2
$geoTargetConstantServiceClient =
    $googleAdsClient->getGeoTargetConstantServiceClient();
$request = (new SuggestGeoTargetConstantsRequest())
    ->setLocale($locale)
    ->setCountryCode($countryCode)
    ->setLocationNames(new LocationNames(['names' => $locationNames]));
$response =
    $geoTargetConstantServiceClient->suggestGeoTargetConstants($request);
      
Kalıp 2 GAPIC v1
N/A
GAPIC v2
$geoTargetConstantServiceClient =
    $googleAdsClient->getGeoTargetConstantServiceClient();
$response = $geoTargetConstantServiceClient->suggestGeoTargetConstants(
    new SuggestGeoTargetConstantsRequest([
        'locale' => $locale,
        'country_code' => $countryCode,
        'location_names' => new LocationNames(['names' => $locationNames])
    ])
);