ระบบจะสร้างซอร์สโค้ดในsrc/Google/Ads/GoogleAds/vX
ไดเรกทอรีของไลบรารีไคลเอ็นต์ PHP ของ Google Ads API โดยอัตโนมัติ ซึ่ง 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/
การใช้งาน
คุณต้องสร้างออบเจ็กต์คำขอและส่งไปยังไคลเอ็นต์ที่ต้องการใช้
ในบางกรณี คุณมีวิธีสร้างออบเจ็กต์คำขอมากกว่า 1 วิธี
เนื่องจากไคลเอ็นต์บางรายก็มีวิธีที่สะดวกชื่อ build()
สำหรับส่ง
พารามิเตอร์ที่จำเป็น
ตัวอย่าง 1.1: เมธอดที่มีพารามิเตอร์ที่ต้องระบุ
โค้ดตัวอย่างต่อไปนี้แสดงวิธีเรียกใช้ CampaignService::mutate()
พารามิเตอร์ทั้งหมด ($customerId
และ $operations
) เป็นพารามิเตอร์ที่ต้องระบุ ดังนั้นระบบจะสร้าง build()
ที่ยอมรับทั้ง 2 พารามิเตอร์ในโค้ด
รูปแบบที่ 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()
ที่สร้างขึ้นในโค้ดจะยอมรับพารามิเตอร์เพียง 2 รายการ ($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]) ]) );