Podstawowe użycie biblioteki klienta wygląda tak:
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.
Tworzenie instancji GoogleAdsClient
Najważniejszą klasą w bibliotece PHP interfejsu Google Ads API jest klasa GoogleAdsClient. Umożliwia tworzenie wstępnie skonfigurowanych obiektów klienta usługi, których można używać do wywoływania interfejsów API. GoogleAdsClient udostępnia różne sposoby tworzenia instancji:
- Użyj pliku
google_ads_php.ini. - Używanie zmiennych środowiskowych.
- Używanie setterów w przypadku elementu
GoogleAdsClientBuilder.
Więcej informacji znajdziesz w przewodniku po konfiguracji.
Aby skonfigurować obiekt GoogleAdsClient, utwórz obiekt OAuth2TokenBuilder i obiekt GoogleAdsClientBuilder i skonfiguruj niezbędne ustawienia:
// 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();
Utwórz usługę
GoogleAdsClient udostępnia metodę pobierania dla każdego obiektu klienta usługi.
Aby na przykład utworzyć instancję CampaignServiceClient, wywołaj metodę GoogleAdsClient->getCampaignServiceClient(), jak pokazano w poprzednim przykładzie.
Obsługa błędów
Nie każde wywołanie interfejsu API zakończy się powodzeniem. Serwer może zgłaszać błędy, jeśli wywołania interfejsu API z jakiegoś powodu się nie powiodą. Ważne jest, aby rejestrować błędy interfejsu API i odpowiednio je obsługiwać.
Gdy wystąpi błąd interfejsu API, zgłaszana jest instancja GoogleAdsException. Zawiera ona szczegółowe informacje, które pomogą Ci ustalić, co poszło nie tak:
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
);
}