קוד המקור בספריית הלקוח של 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]) ]) );