Utilizzo di base

L'utilizzo di base della libreria client è il seguente:

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.

Crea un'istanza di GoogleAdsClient

La classe più importante nella libreria PHP dell'API Google Ads è la classe GoogleAdsClient. Ti consente di creare oggetti client di servizio preconfigurati che possono essere utilizzati per effettuare chiamate API. GoogleAdsClient offre vari modi per istanziarlo:

  • Utilizza un file google_ads_php.ini.
  • Utilizza le variabili di ambiente.
  • Utilizza i setter su GoogleAdsClientBuilder.

Per saperne di più, consulta la guida alla configurazione.

Per configurare un oggetto GoogleAdsClient, crea un OAuth2TokenBuilderoggetto e un GoogleAdsClientBuilder oggetto e imposta le impostazioni necessarie:

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

Crea un servizio

GoogleAdsClient fornisce un metodo getter per ogni oggetto client di servizio. Ad esempio, per creare un'istanza di CampaignServiceClient, chiama il metodo GoogleAdsClient->getCampaignServiceClient(), come mostrato nell' esempio precedente.

Gestione degli errori

Non tutte le chiamate API andranno a buon fine. Il server può generare errori se le chiamate API non vanno a buon fine per qualche motivo. È importante acquisire gli errori API e gestirli in modo appropriato.

Quando si verifica un errore API, viene generata un'istanza di GoogleAdsException. Contiene dettagli che ti aiutano a capire cosa non è andato per il verso giusto:

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