Dưới đâ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\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.
Khởi chạy ứng dụng và dịch vụ
Để đưa ra yêu cầu đến Google Ads API, trước tiên, hãy khởi chạy một thực thể ứng dụng, sau đó dùng thực thể đó để 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 Google Ads API PHP là lớp GoogleAdsClient. Lớp này cho phép bạn tạo các đối tượng ứng dụng dịch vụ được định cấu hình sẵn mà bạn có thể dùng để thực hiện 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ể:
- 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 định 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, sau đó 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ì một số lý do. Bạn cần nắm bắt các lỗi API và xử lý chúng 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ực thể này chứa thông tin chi tiết giúp bạn tìm hiểu những vấn đề đã xảy ra:
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",
$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
);
}