โค้ดซอร์สในไดเรกทอรี 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]) ]) );