שימוש בסיסי

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

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

ה-class הכי חשוב בספריית ה-PHP של Google Ads API הוא GoogleAdsClient. הוא מאפשר ליצור אובייקטים של לקוחות שירות שהוגדרו מראש, שאפשר להשתמש בהם כדי לבצע קריאות ל-API. יש כמה דרכים להגדיר את GoogleAdsClient וליצור מופע שלו:

  • משתמשים בקובץ google_ads_php.ini.
  • מגדירים את משתני הסביבה.
  • מפעילים שיטות 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_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, צריך להפעיל את ה-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",
        $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
    );
}