Temel kullanım

İstemci kitaplığının temel kullanımı aşağıdaki gibidir:

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.

İstemci ve hizmet başlatma

Google Ads API'ye istekte bulunmak için önce bir istemci örneğini başlatın, ardından bir hizmet istemcisi oluşturmak için bu örneği kullanın.

GoogleAdsClient örneği oluşturma

Google Ads API PHP kitaplığındaki en önemli sınıf GoogleAdsClient sınıfıdır. API çağrıları yapmak için kullanılabilecek önceden yapılandırılmış hizmet istemcisi nesneleri oluşturmanıza olanak tanır. GoogleAdsClient, yapılandırmak ve örneklemek için çeşitli yöntemler sunar:

  • google_ads_php.ini dosyası kullanın.
  • Ortam değişkenlerini ayarlayın.
  • GoogleAdsClientBuilder üzerinde belirleyici yöntemleri çağırın.

Daha fazla bilgi için Yapılandırma kılavuzu'na bakın.

GoogleAdsClient nesnesini açık ayarlar kullanarak yapılandırmak için bir OAuth2TokenBuilder nesnesi ve bir GoogleAdsClientBuilder nesnesi oluşturun ve uygun belirleyici yöntemleri çağırın:

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

Hizmet oluştur

GoogleAdsClient, her hizmet istemci nesnesi için bir getter yöntemi sağlar. Örneğin, CampaignServiceClient örneği oluşturmak için önceki örnekte gösterildiği gibi $googleAdsClient->getCampaignServiceClient() yöntemini çağırın.

Hata işleme

Her API çağrısı başarılı olmaz. API çağrılarınız herhangi bir nedenle başarısız olursa sunucu hata verebilir. API hatalarını yakalamak ve uygun şekilde ele almak önemlidir.

API hatası oluştuğunda GoogleAdsException örneği oluşturulur. Bu mesajda, neyin yanlış gittiğini anlamanıza yardımcı olacak ayrıntılar yer alır:

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