Podstawowe użycie biblioteki klienta wygląda tak:
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.
Tworzenie instancji GoogleAdsClient
Najważniejszą klasą w bibliotece PHP interfejsu Google Ads API jest klasa GoogleAdsClient. Umożliwia ona tworzenie wstępnie skonfigurowanych obiektów klienta usługi, których można używać do wykonywania wywołań interfejsu 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
GoogleAdsClientBuilder.
Więcej informacji znajdziesz w przewodniku po konfiguracji.
Aby skonfigurować obiekt GoogleAdsClient, utwórz obiekt
OAuth2TokenBuilder i obiekt GoogleAdsClientBuilder
oraz ustaw 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();
Tworzenie usługi
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. Jeśli wywołania interfejsu API nie powiodą się z jakiegoś powodu, serwer może zgłosić błędy. Ważne jest, aby przechwytywać błędy interfejsu API i odpowiednio je obsługiwać.
Gdy wystąpi błąd interfejsu API, zgłaszana jest instancja GoogleAdsException. Zawiera ona szczegóły, które pomogą Ci ustalić, co poszło nie tak:
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
);
}