Penggunaan dasar

Penggunaan dasar library klien adalah sebagai berikut:

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

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 berbagai cara untuk membuat instance-nya:

  • Menggunakan file google_ads_php.ini.
  • Menggunakan variabel lingkungan.
  • Menggunakan setter di GoogleAdsClientBuilder.

Lihat Panduan konfigurasi untuk mempelajari lebih lanjut.

Untuk mengonfigurasi objek GoogleAdsClient, buat objek OAuth2TokenBuilder dan objek GoogleAdsClientBuilder , lalu tetapkan setelan yang diperlukan:

// 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();

Membuat service

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 akan 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 memiliki detail untuk membantu Anda mengetahui penyebab masalah:

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