GAPIC

ระบบจะสร้างซอร์สโค้ดในไดเรกทอรี 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/ (เช่น V25) และคําขอที่เกี่ยวข้องและ คลาสข้อความตอบกลับ (เช่น MutateCampaignsRequest และ SearchGoogleAdsRequest) จะอยู่ใน src/Google/Ads/GoogleAds/VX/Services/ การเปิดตัว API เวอร์ชันย่อย (เช่น v25.1) จะสร้างขึ้นภายในไดเรกทอรีเวอร์ชันหลัก (V25)

การใช้งาน

คุณต้องสร้างออบเจ็กต์คำขอและส่งไปยังไคลเอ็นต์บริการที่ต้องการใช้ คุณมีหลายวิธีในการสร้างออบเจ็กต์คำขอ ซึ่งรวมถึงbuild()เมธอด Factory แบบคงที่ สำหรับส่งพารามิเตอร์ที่จำเป็น ทั้งนี้ขึ้นอยู่กับว่าพารามิเตอร์นั้นจำเป็นหรือไม่

ตัวอย่าง 1.1: เมธอดที่มีพารามิเตอร์ที่ต้องระบุ

โค้ดตัวอย่างต่อไปนี้แสดงวิธีเรียกใช้ CampaignServiceClient::mutateCampaigns() เนื่องจากทั้ง $customerId และ $campaignOperations เป็นพารามิเตอร์ที่จำเป็น ระบบจึงสร้างเมธอดแบบคงที่ 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() จึงไม่สร้างในซอร์สโค้ดในกรณีนี้ คุณต้องสร้างออบเจ็กต์คำขอโดยใช้ Setter หรือตัวสร้างอาร์เรย์

รูปแบบที่ 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])
    ])
);