ซอร์สโค้ดในไดเรกทอรี src/Google/Ads/GoogleAds/VX ของไลบรารีของไคลเอ็นต์ PHP สำหรับ Google Ads API โดยที่ 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/ (เช่น V26) และคลาสข้อความคำขอและการตอบกลับที่เกี่ยวข้อง (เช่น MutateCampaignsRequest และ SearchGoogleAdsRequest) จะอยู่ใน src/Google/Ads/GoogleAds/VX/Services/ API เวอร์ชันย่อย (เช่น v25.1) จะสร้างขึ้นภายในไดเรกทอรีเวอร์ชันหลัก (V25)
การใช้งาน
คุณต้องสร้างออบเจ็กต์คำขอและส่งไปยังไคลเอ็นต์บริการที่ต้องการใช้ คุณสร้างออบเจ็กต์คำขอได้หลายวิธี โดยขึ้นอยู่กับว่าพารามิเตอร์เป็นแบบจำเป็นหรือไม่บังคับ ซึ่งรวมถึงเมธอดแฟกทอรี build() แบบคงที่สำหรับการส่งพารามิเตอร์ที่จำเป็น
ตัวอย่างที่ 1.1: เมธอดที่มีพารามิเตอร์ที่จำเป็น
โค้ดตัวอย่างต่อไปนี้แสดงวิธีเรียกใช้ CampaignServiceClient::mutateCampaigns() เนื่องจากทั้ง $customerId และ $campaignOperations เป็นพารามิเตอร์ที่จำเป็น เมธอดแฟกทอรี build() แบบคงที่ซึ่งยอมรับพารามิเตอร์ทั้ง 2 รายการจึงสร้างขึ้นในคลาสคำขอ
รูปแบบที่ 1: วิธีการสร้างแบบคงที่ (แนะนำ)
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns(
MutateCampaignsRequest::build(
$customerId,
$campaignOperations
)
);
รูปแบบที่ 2: เมธอด Setter แบบต่อเนื่อง
$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: เมธอดการสร้างแบบคงที่พร้อม Setter ที่คล่องตัว
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
$response = $googleAdsServiceClient->search(
SearchGoogleAdsRequest::build($customerId, $query)
->setReturnTotalResultsCount(true)
);
รูปแบบที่ 2: เมธอด Setter แบบต่อเนื่อง
$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() ในซอร์สโค้ดในกรณีนี้ คุณต้องสร้างออบเจ็กต์คำขอโดยใช้ Setter หรือ Array Constructor
รูปแบบที่ 1: เมธอด Setter ที่คล่องแคล่ว (แนะนำ)
$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])
])
);