שימוש בסיסי

כך משתמשים בספריית הלקוח:

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

  • משתמשים בקובץ google_ads_php.ini.
  • מגדירים משתני סביבה.
  • מפעילים את ה-methods של ה-setter ב-GoogleAdsClientBuilder.

מידע נוסף זמין במדריך להגדרה.

כדי להגדיר אובייקט GoogleAdsClient עם הגדרות מפורשות, יוצרים אובייקט OAuth2TokenBuilder ואובייקט GoogleAdsClientBuilder ושולחים קריאה ל-methods המתאימות של ה-setter:

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