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\OAuth2TokenBuilder;
use Google\Ads\GoogleAds\Lib\V25\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.

Khởi chạy máy khách và dịch vụ

Để đưa ra yêu cầu cho Google Ads API, trước tiên, hãy khởi chạy một phiên bản ứng dụng và sau đó dùng phiên bản đó để tạo một ứng dụng dịch vụ.

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 các lệnh gọi API. GoogleAdsClient cung cấp một số cách để định cấu hình và tạo thực thể cho nó:

  • Sử dụng tệp google_ads_php.ini.
  • Đặt các biến môi trường.
  • Gọi các phương thứ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 bằng các chế độ cài đặt rõ ràng, hãy tạo đối tượng OAuth2TokenBuilder và đối tượng GoogleAdsClientBuilder rồi gọi các phương thức setter thích hợp:

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

// Construct a Google Ads client configured from explicit settings and the
// OAuth 2.0 credentials.
$googleAdsClient = (new GoogleAdsClientBuilder())
    ->withOAuth2Credential($oAuth2Credential)
    // Replace 1234567890 with your login customer ID.
    ->withLoginCustomerId(1234567890)
    ->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 đó. Điều quan trọng là bạn phải 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 tin này chứa các chi tiết giúp bạn xác định vấn đề:

use Google\Ads\GoogleAds\Lib\V25\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
    );
}