Penggunaan dasar

Penggunaan dasar library klien adalah sebagai berikut:

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.

Inisialisasi klien dan layanan

Untuk membuat permintaan ke Google Ads API, pertama-tama inisialisasi instance klien, lalu gunakan untuk membuat klien layanan.

Membuat instance GoogleAdsClient

Class terpenting dalam library PHP Google Ads API adalah class GoogleAdsClient. Class ini memungkinkan Anda membuat objek klien layanan yang telah dikonfigurasi sebelumnya yang dapat digunakan untuk melakukan panggilan API. GoogleAdsClient menyediakan beberapa cara untuk mengonfigurasi dan membuat instance:

  • Menggunakan file google_ads_php.ini.
  • Menetapkan variabel lingkungan.
  • Memanggil metode setter di 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 layanan

GoogleAdsClient menyediakan metode getter untuk setiap objek klien layanan. Misalnya, untuk membuat instance CampaignServiceClient, panggil metode $googleAdsClient->getCampaignServiceClient(), seperti yang ditunjukkan dalam contoh sebelumnya.

Penanganan error

Tidak semua panggilan API berhasil. Server dapat menampilkan error jika panggilan API Anda gagal karena alasan tertentu. Penting untuk menangkap error API dan menanganinya dengan tepat.

Instance GoogleAdsException ditampilkan saat terjadi error API. Instance ini berisi detail untuk membantu Anda mengetahui penyebab masalah:

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
    );
}