ซอร์สโค้ดใน src/Google/Ads/GoogleAds/vX
ของไลบรารีไคลเอ็นต์ Google Ads API PHP โดยที่ 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]) ]) );