في ما يلي الاستخدام الأساسي لمكتبة العميل:
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;
use Google\Ads\GoogleAds\Lib\V26\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.
تهيئة العميل والخدمة
لإرسال طلبات إلى Google Ads API، عليك أولاً تهيئة مثيل عميل، ثم استخدامه لإنشاء عميل خدمة.
إنشاء مثيل GoogleAdsClient
الفئة الأكثر أهمية في مكتبة Google Ads API PHP هي فئة GoogleAdsClient. تتيح لك هذه الفئة إنشاء كائنات عميل خدمة مضبوطة مسبقًا يمكن استخدامها لإجراء طلبات البيانات من واجهة برمجة التطبيقات. توفّر فئة GoogleAdsClient عدة طرق لضبطها وإنشاء مثيل لها:
- استخدام ملف
google_ads_php.ini - ضبط متغيرات البيئة
- استدعاء طرق الضبط في
GoogleAdsClientBuilder
يُرجى الرجوع إلى دليل الإعداد لمزيد من المعلومات.
لضبط كائن GoogleAdsClient باستخدام إعدادات صريحة، أنشئ كائن
OAuth2TokenBuilder وكائن GoogleAdsClientBuilder واستدعِ طرق الضبط المناسبة:
// 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();
إنشاء خدمة
توفّر فئة GoogleAdsClient طريقة getter لكل كائن عميل خدمة.
على سبيل المثال، لإنشاء مثيل من CampaignServiceClient، استدعِ الطريقة
$googleAdsClient->getCampaignServiceClient()، كما هو موضّح في الـ
مثال السابق.
معالجة الأخطاء
لا تنجح جميع طلبات البيانات من واجهة برمجة التطبيقات. يمكن أن يعرض الخادم أخطاءً إذا تعذّر تنفيذ طلبات البيانات من واجهة برمجة التطبيقات لسبب ما. من المهم رصد أخطاء واجهة برمجة التطبيقات ومعالجتها بشكل مناسب.
يتم عرض مثيل GoogleAdsException عند حدوث خطأ في واجهة برمجة التطبيقات. ويحتوي على تفاصيل لمساعدتك في تحديد المشكلة:
use Google\Ads\GoogleAds\Lib\V26\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
);
}