GAPIC

קוד המקור בספריית הלקוח של Google Ads API ל-PHP, שמופיע בספרייה src/Google/Ads/GoogleAds/vX (כאשר X הוא גרסת Google Ads API), נוצר באופן אוטומטי באמצעות הגנרטור של GAPIC (לקוח API שנוצר), על סמך קובצי ה-proto שפורסמו.

לאחר מכן, קוד המקור שנוצר משתנה כך שיכיל הפניות למאפיינים ולכיתות הנדרשים ליצירת לקוחות השירות שפועלים עם Google Ads API באמצעות הכיתה GoogleAdsClient, שנוצרת על ידי קריאה ל-GoogleAdsClientBuilder::build(). גם הכיתה 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])
    ])
);