קוד המקור בספריית src/Google/Ads/GoogleAds/vX
של ספריית הלקוח של Google Ads API PHP, כאשר X הוא הגרסה של Google Ads API, נוצר באופן אוטומטי באמצעות הכלי ליצירת GAPIC (Generated API Client), על סמך קבצי ה-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()
. לחלופין, אפשר להעביר את כל הפרמטרים יחד אל ה-constructor של 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]) ]) );