GAPIC

src/Google/Ads/GoogleAds/VX dizinindeki kaynak kodu (Google Ads API PHP istemci kitaplığının X Google Ads API sürüm numarasıdır), yayınlanmış proto dosyalarına dayalı olarak GAPIC (Oluşturulmuş API İstemcisi) Oluşturucu kullanılarak otomatik olarak oluşturulur.

Oluşturulan kaynak kodu daha sonra GoogleAdsClient sınıfı kullanılarak 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. sınıfı, GoogleAdsClientBuilder::build() çağrılarak oluşturulur. Hem GoogleAdsClient hem de GoogleAdsClientBuilder, src/Google/Ads/GoogleAds/Lib/VX/ içinde bulunan 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, V25) içinde, ilgili istek ve yanıt mesajı sınıfları (örneğin, MutateCampaignsRequest ve SearchGoogleAdsRequest) ise src/Google/Ads/GoogleAds/VX/Services/ içinde bulunur. Küçük API sürümleri (ör. v25.1), ana sürüm dizinlerinde (V25) oluşturulur.

Kullanım

Bir istek nesnesi oluşturup kullanmak istediğiniz hizmet istemcisine iletmeniz gerekir. Parametrelerin gerekli veya isteğe bağlı olmasına bağlı olarak, gerekli parametreleri iletmek için statik build() fabrika yöntemi de dahil olmak üzere istek nesnesi oluşturmanın birden fazla yolu vardır.

Örnek 1.1: Zorunlu parametreler içeren yöntemler

Aşağıdaki örnek kodda CampaignServiceClient::mutateCampaigns() işlevinin 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. desen: Statik derleme yöntemi (önerilir)

$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns(
    MutateCampaignsRequest::build(
        $customerId,
        $campaignOperations
    )
);

Desen 2: Akıcı ayarlayıcılar

$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$request = (new MutateCampaignsRequest())
    ->setCustomerId($customerId)
    ->setCampaignOperations($campaignOperations);
$response = $campaignServiceClient->mutateCampaigns($request);

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

Aşağıdaki örnek kodda GoogleAdsServiceClient::search() çağrısı yapılmaktadır. Bu örnekte, oluşturulan build() yöntemi yalnızca iki parametre ($customerId ve $query) kabul eder. Bunun nedeni, bu parametrelerin zorunlu olmasıdır. LIMIT ifadesini yoksayarak sorguyla eşleşen toplam sonuç sayısını istemek için setReturnTotalResultsCount() kullanarak açıkça ayarlayabilirsiniz. Alternatif olarak, 3. desende gösterildiği gibi tüm parametreleri SearchGoogleAdsRequest oluşturucusuna ilişkisel bir dizide birlikte iletebilirsiniz.

1. desen: Akıcı ayarlayıcılarla statik oluşturma yöntemi

$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
$response = $googleAdsServiceClient->search(
    SearchGoogleAdsRequest::build($customerId, $query)
        ->setReturnTotalResultsCount(true)
);

Desen 2: Akıcı ayarlayıcılar

$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
$request = (new SearchGoogleAdsRequest())
    ->setCustomerId($customerId)
    ->setQuery($query)
    ->setReturnTotalResultsCount(true);
$response = $googleAdsServiceClient->search($request);

3. desen: 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() işlevinin nasıl çağrılacağı gösterilmektedir. GeoTargetConstantServiceClient::suggestGeoTargetConstants() parametrelerinin tümü isteğe bağlı olduğundan bu durumda kaynak kodda build() oluşturulmaz. İsteği, 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);

Desen 2: Dizi oluşturucu

$geoTargetConstantServiceClient =
    $googleAdsClient->getGeoTargetConstantServiceClient();
$response = $geoTargetConstantServiceClient->suggestGeoTargetConstants(
    new SuggestGeoTargetConstantsRequest([
        'locale' => $locale,
        'country_code' => $countryCode,
        'location_names' => new LocationNames(['names' => $locationNames])
    ])
);