کاربرد اصلی کتابخانه کلاینت به شرح زیر است:
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.
یک نمونه GoogleAdsClient ایجاد کنید
مهمترین کلاس در کتابخانه PHP API گوگل ادز، کلاس GoogleAdsClient است. این کلاس به شما امکان میدهد اشیاء کلاینت سرویس از پیش پیکربندیشدهای ایجاد کنید که میتوانند برای فراخوانیهای API استفاده شوند. GoogleAdsClient روشهای مختلفی برای نمونهسازی آن ارائه میدهد:
- از فایل
google_ads_php.iniاستفاده کنید. - استفاده از متغیرهای محیطی
- استفاده از تنظیمکنندهها در
GoogleAdsClientBuilder.
برای کسب اطلاعات بیشتر به راهنمای پیکربندی مراجعه کنید.
برای پیکربندی یک شیء GoogleAdsClient ، یک شیء OAuth2TokenBuilder و یک شیء 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
$googleAdsClient = (new GoogleAdsClientBuilder())
->fromFile()
->withOAuth2Credential($oAuth2Credential)
->withLoginCustomerId(1234567890) // Replace 1234567890 with your login customer ID.
->build();
ایجاد یک سرویس
GoogleAdsClient برای هر شیء سرویس کلاینت یک متد getter ارائه میدهد. برای مثال، برای ایجاد یک نمونه از CampaignServiceClient ، متد GoogleAdsClient->getCampaignServiceClient() را همانطور که در مثال قبلی نشان داده شده است، فراخوانی کنید.
مدیریت خطا
هر فراخوانی API موفق نخواهد بود. اگر فراخوانیهای API شما به هر دلیلی با شکست مواجه شوند، سرور میتواند خطا صادر کند. مهم است که خطاهای API را ثبت کرده و به طور مناسب آنها را مدیریت کنید.
یک نمونه از GoogleAdsException زمانی ایجاد میشود که یک خطای API رخ دهد. این نمونه شامل جزئیاتی است که به شما کمک میکند بفهمید چه چیزی اشتباه رخ داده است:
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
);
}