L'utilizzo di base della libreria client è il seguente:
use Google\Ads\GoogleAds\Lib\V22\GoogleAdsClient;
use Google\Ads\GoogleAds\Lib\V22\GoogleAdsClientBuilder;
use Google\Ads\GoogleAds\Lib\V22\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 GoogleAdsClient
La classe più importante nella libreria PHP dell'API Google Ads è la classe
GoogleAdsClient. Consente di creare oggetti client di servizio preconfigurati
che possono essere utilizzati per effettuare chiamate API. GoogleAdsClient fornisce
vari modi per istanziarlo:
- Utilizza un file
google_ads_php.ini. - Utilizzo delle variabili di ambiente.
- Utilizzo dei setter su
GoogleAdsClientBuilder.
Per saperne di più, consulta la guida alla configurazione.
Per configurare un oggetto GoogleAdsClient, crea un oggetto OAuth2TokenBuilder e un oggetto GoogleAdsClientBuilder 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.
Viene generata un'istanza GoogleAdsException quando si verifica un errore API. Contiene
dettagli per aiutarti a capire cosa è andato storto:
use Google\Ads\GoogleAds\Lib\V22\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
);
}