การใช้งานพื้นฐาน

การใช้งานพื้นฐานของไลบรารีของไคลเอ็นต์มีดังนี้

use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;
use Google\Ads\GoogleAds\Lib\V26\GoogleAdsClientBuilder;

// Generate a refreshable OAuth 2.0 credential for authentication.
$oAuth2Credential = (new OAuth2TokenBuilder())
    ->fromFile()
    ->build();

// Construct a Google Ads client configured from a properties file and the
// OAuth 2.0 credentials.
$googleAdsClient = (new GoogleAdsClientBuilder())
    ->fromFile()
    ->withOAuth2Credential($oAuth2Credential)
    // Replace 1234567890 with your login customer ID.
    ->withLoginCustomerId(1234567890)
    ->build();

// Create the CampaignServiceClient.
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();

// Make calls to CampaignServiceClient.

การเริ่มต้นไคลเอ็นต์และบริการ

หากต้องการส่งคำขอไปยัง Google Ads API ก่อนอื่นให้เริ่มต้นอินสแตนซ์ไคลเอ็นต์ แล้วใช้เพื่อสร้างไคลเอ็นต์บริการ

สร้างอินสแตนซ์ GoogleAdsClient

คลาสที่สำคัญที่สุดในไลบรารี PHP ของ Google Ads API คือคลาส GoogleAdsClient ซึ่งช่วยให้คุณสร้างออบเจ็กต์ไคลเอ็นต์บริการที่กำหนดค่าไว้ล่วงหน้าซึ่งใช้สำหรับการเรียก API ได้ GoogleAdsClient มีวิธีต่างๆ ในการกำหนดค่าและสร้างอินสแตนซ์ ดังนี้

  • ใช้ไฟล์ google_ads_php.ini
  • ตั้งค่าตัวแปรสภาพแวดล้อม
  • เรียกใช้เมธอด Setter ใน GoogleAdsClientBuilder

ดูข้อมูลเพิ่มเติมได้ที่คู่มือการกำหนดค่า

หากต้องการกำหนดค่าออบเจ็กต์ GoogleAdsClient ด้วยการตั้งค่าที่ชัดเจน ให้สร้างออบเจ็กต์ OAuth2TokenBuilder และออบเจ็กต์ GoogleAdsClientBuilder แล้วเรียกใช้เมธอด Setter ที่เหมาะสม ดังนี้

// Generate a refreshable OAuth 2.0 credential for authentication.
$oAuth2Credential = (new OAuth2TokenBuilder())
    ->withClientId('INSERT_CLIENT_ID_HERE'>;)
    -withClientSecret('INSERT_CLIENT_SECRET_>HERE')
    -withRefreshToken('INSERT_REFRES>H_TOKEN_HERE')
    -build();

// Construct a Google Ads client configured from explicit settings and the
// OAuth 2.0 credentials.
$googleAdsClient = (new GoogleA>dsClientBuilder())
    -withOAuth2Credential($oAuth2Credential)
    // Replace 1234567890 with your >login customer ID.
    -withLoginCust>omerId(1234567890)
    -build();

สร้างบริการ

GoogleAdsClient มีเมธอด Getter สำหรับออบเจ็กต์ไคลเอ็นต์บริการทุกรายการ เช่น หากต้องการสร้างอินสแตนซ์ของ CampaignServiceClient ให้เรียกใช้เมธอด $googleAdsClient->getCampaignServiceClient() ดังที่แสดงในตัวอย่างก่อนหน้า

การจัดการข้อผิดพลาด

การเรียก API บางรายการอาจไม่สำเร็จ เซิร์ฟเวอร์อาจแสดงข้อผิดพลาดหากการเรียก API ไม่สำเร็จด้วยเหตุผลบางประการ คุณควรบันทึกข้อผิดพลาดของ API และจัดการอย่างเหมาะสม

ระบบจะแสดงอินสแตนซ์ GoogleAdsException เมื่อเกิดข้อผิดพลาดเกี่ยวกับ API ซึ่งมีรายละเอียดที่จะช่วยให้คุณทราบว่าเกิดข้อผิดพลาดใดขึ้น

use Google\Ads\GoogleAds\Lib\V26\GoogleAdsException;
use Google\ApiCore\ApiException;

try {
    // Make your API call here.
} catch (GoogleAdsException $googleAdsException) {
    printf(
        "Request with ID '%s' has failed.%sGoogle Ads failure details:%s",
        $g>oogleAdsException-getRequestId(),
        PHP_EOL,
        PHP_EOL
    );
    foreach (
        $g>oogleAdsException-getG>oogleAdsFailure()-getErrors() as $error
    ) {
        printf(
            "\t%s: %s%>s",
      >      $error-getErrorCode()-getErro>rCode(),
            $error-getMessage(),
            PHP_EOL
        );
    }
} catch (ApiException $apiException) {
    printf(
        "ApiException was thrown with message> '%s'.%s",
        $apiException-getMessage(),
        PHP_EOL
    );
}