Penggunaan dasar library klien adalah sebagai berikut:
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.
Inisialisasi klien dan layanan
Untuk membuat permintaan ke Google Ads API, inisialisasi instance klien terlebih dahulu, lalu gunakan instance tersebut untuk membuat klien layanan.
Buat instance GoogleAdsClient
Class terpenting di library PHP Google Ads API adalah class GoogleAdsClient. Dengan library ini, Anda dapat membuat objek klien layanan yang telah dikonfigurasi sebelumnya yang dapat digunakan untuk melakukan panggilan API. GoogleAdsClient menyediakan beberapa cara untuk mengonfigurasi dan membuat instance-nya:
- Gunakan file
google_ads_php.ini. - Menetapkan variabel lingkungan.
- Panggil metode setter pada
GoogleAdsClientBuilder.
Lihat Panduan konfigurasi untuk mempelajari lebih lanjut.
Untuk mengonfigurasi objek GoogleAdsClient dengan setelan eksplisit, buat objek
OAuth2TokenBuilder dan objek GoogleAdsClientBuilder, lalu panggil
metode setter yang sesuai:
// 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();
Membuat service
GoogleAdsClient menyediakan metode getter untuk setiap objek klien layanan.
Misalnya, untuk membuat instance CampaignServiceClient, panggil
metode $googleAdsClient->getCampaignServiceClient(), seperti yang ditunjukkan pada
contoh sebelumnya.
Penanganan error
Tidak semua panggilan API berhasil. Server dapat menampilkan error jika panggilan API Anda gagal karena alasan tertentu. Penting untuk mencatat error API dan menanganinya dengan tepat.
Instance GoogleAdsException ditampilkan saat terjadi error API. Log ini berisi detail untuk membantu Anda mengetahui apa yang salah:
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
);
}