Cách dùng cơ bản

Sau đây là cách sử dụng cơ bản của thư viện ứng dụng:

use Google\Ads\GoogleAds\Lib\V22\GoogleAdsClient;
use Google\Ads\GoogleAds\Lib\V22\GoogleAdsClientBuilder;
use Google\Ads\GoogleAds\Lib\V22\GoogleAdsException;
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;
use Google\ApiCore\ApiException;

// 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
$googleAdsClient = (new GoogleAdsClientBuilder())
    ->fromFile()
    ->withOAuth2Credential($oAuth2Credential)
    ->withLoginCustomerId(1234567890) // Replace 1234567890 with your login customer ID.
    ->build();

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

// Make calls to CampaignServiceClient.

Tạo một thực thể GoogleAdsClient

Lớp quan trọng nhất trong thư viện PHP của Google Ads API là lớp GoogleAdsClient. Thư viện này cho phép bạn tạo các đối tượng ứng dụng khách dịch vụ được định cấu hình trước mà bạn có thể dùng để thực hiện lệnh gọi API. GoogleAdsClient cung cấp nhiều cách để khởi tạo:

  • Sử dụng tệp google_ads_php.ini.
  • Sử dụng biến môi trường.
  • Sử dụng các setter trên GoogleAdsClientBuilder.

Hãy tham khảo Hướng dẫn về cấu hình để tìm hiểu thêm.

Để định cấu hình đối tượng GoogleAdsClient, hãy tạo một đối tượng OAuth2TokenBuilder và một đối tượng GoogleAdsClientBuilder, đồng thời đặt các chế độ cài đặt cần thiết:

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

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

Tạo một dịch vụ

GoogleAdsClient cung cấp một phương thức getter cho mọi đối tượng ứng dụng dịch vụ. Ví dụ: để tạo một thực thể của CampaignServiceClient, hãy gọi phương thức GoogleAdsClient->getCampaignServiceClient(), như trong ví dụ trước.

Xử lý lỗi

Không phải lệnh gọi API nào cũng thành công. Máy chủ có thể gửi lỗi nếu lệnh gọi API của bạn không thành công vì lý do nào đó. Bạn cần nắm bắt và xử lý các lỗi API một cách thích hợp.

Một thực thể GoogleAdsException sẽ được gửi khi xảy ra lỗi API. Thông báo này có thông tin chi tiết để giúp bạn xác định vấn đề:

use Google\Ads\GoogleAds\Lib\V22\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",
        $googleAdsException->getRequestId(),
        PHP_EOL,
        PHP_EOL
    );
    foreach ($googleAdsException->getGoogleAdsFailure()->getErrors() as $error) {
        printf(
            "\t%s: %s%s",
            $error->getErrorCode()->getErrorCode(),
            $error->getMessage(),
            PHP_EOL
        );
    }
} catch (ApiException $apiException) {
    printf(
        "ApiException was thrown with message '%s'.%s",
        $apiException->getMessage(),
        PHP_EOL
    );
}