L'utilizzo di base della libreria client è il seguente:
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.
Inizializzazione di client e servizi
Per effettuare richieste all'API Google Ads, inizializza prima un'istanza client e poi utilizzala per creare un client di servizio.
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 offre diversi modi per configurarlo e
crearlo:
- Utilizza un file
google_ads_php.ini. - Imposta le variabili di ambiente.
- Chiama i metodi setter su
GoogleAdsClientBuilder.
Per saperne di più, consulta la guida alla configurazione.
Per configurare un oggetto GoogleAdsClient con impostazioni esplicite, crea un oggetto OAuth2TokenBuilder e un oggetto GoogleAdsClientBuilder e chiama i metodi setter appropriati:
// 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();
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 vanno 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 che ti aiutano a capire cosa è andato storto:
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
);
}