שימוש בסיסי

השימוש הבסיסי בספריית הלקוח הוא כדלקמן:

use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;
use Google\Ads\GoogleAds\Lib\V25\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 class. הוא מאפשר ליצור אובייקטים של לקוחות שירות שהוגדרו מראש, שאפשר להשתמש בהם כדי לבצע קריאות ל-API. ‫GoogleAdsClient מספק כמה דרכים להגדיר אותו וליצור ממנו מופע:

  • משתמשים בקובץ google_ads_php.ini.
  • מגדירים משתני סביבה.
  • מבצעים קריאה ל-setter methods ב-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, צריך להפעיל את ה-method‏ $googleAdsClient->getCampaignServiceClient(), כמו בדוגמה הקודמת.

טיפול בשגיאות

לא כל קריאה ל-API מצליחה. השרת יכול להחזיר שגיאות אם הקריאות ל-API נכשלות מסיבה כלשהי. חשוב לתעד שגיאות ב-API ולטפל בהן בצורה מתאימה.

מופעלת דוגמה של GoogleAdsException כשמתרחשת שגיאת API. הוא מכיל פרטים שיעזרו לכם להבין מה הייתה הבעיה:

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
    );
}