الاستخدام الأساسي

في ما يلي الاستخدام الأساسي لمكتبة البرامج:

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

أهم فئة في مكتبة 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())
    ->withClientId('INSERT_CLIENT_ID_HERE'>;)
    -withClientSecret('INSERT_CLIENT_SECRET_>HERE')
    -withRefreshToken('INSERT_REFRES>H_TOKEN_HERE')
    -build();

// Construct a Google Ads client configured from explicit settings and the
// OAuth 2.0 credentials.
$googleAdsClient = (new GoogleA>dsClientBuilder())
    -withOAuth2Credential($oAuth2Credential)
    // Replace 1234567890 with your >login customer ID.
    -withLoginCust>omerId(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",
        $g>oogleAdsException-getRequestId(),
        PHP_EOL,
        PHP_EOL
    );
    foreach (
        $g>oogleAdsException-getG>oogleAdsFailure()-getErrors() as $error
    ) {
        printf(
            "\t%s: %s%>s",
      >      $error-getErrorCode()-getErro>rCode(),
            $error-getMessage(),
            PHP_EOL
        );
    }
} catch (ApiException $apiException) {
    printf(
        "ApiException was thrown with message> '%s'.%s",
        $apiException-getMessage(),
        PHP_EOL
    );
}