في ما يلي الاستخدام الأساسي لمكتبة البرامج:
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 الخاصة بواجهة برمجة التطبيقات Google Ads API هي الفئة
GoogleAdsClient. تتيح لك إنشاء عناصر عميل خدمة مضبوطة مسبقًا يمكن استخدامها لإجراء طلبات إلى واجهة برمجة التطبيقات. توفّر 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()، كما هو موضّح في المثال السابق.
معالجة الأخطاء
لن تنجح جميع طلبات البيانات من واجهة برمجة التطبيقات. يمكن أن يعرض الخادم أخطاءً إذا تعذّرت معالجة طلبات البيانات من واجهة برمجة التطبيقات لسبب ما. من المهم تسجيل أخطاء واجهة برمجة التطبيقات والتعامل معها بشكل مناسب.
يتم عرض مثيل GoogleAdsException عند حدوث خطأ في واجهة برمجة التطبيقات. يتضمّن هذا الرمز تفاصيل تساعدك في معرفة المشكلة التي حدثت:
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
);
}