बुनियादी इस्तेमाल

क्लाइंट लाइब्रेरी का बुनियादी इस्तेमाल इस तरह किया जाता है:

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 इंस्टेंस बनाना

Google Ads API की PHP लाइब्रेरी में सबसे अहम क्लास, 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_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, हर सेवा क्लाइंट ऑब्जेक्ट के लिए एक गेटर मेथड उपलब्ध कराता है. उदाहरण के लिए, CampaignServiceClient का इंस्टेंस बनाने के लिए, $googleAdsClient->getCampaignServiceClient() तरीके को कॉल करें. इसे यहां दिए गए उदाहरण में दिखाया गया है.

गड़बड़ी ठीक करना

ऐसा ज़रूरी नहीं है कि हर एपीआई कॉल पूरा हो. अगर किसी वजह से आपके एपीआई कॉल पूरे नहीं हो पाते हैं, तो सर्वर गड़बड़ियां दिखा सकता है. एपीआई की गड़बड़ियों को कैप्चर करना और उन्हें सही तरीके से मैनेज करना ज़रूरी है.

एपीआई से जुड़ी गड़बड़ी होने पर, GoogleAdsException इंस्टेंस थ्रो किया जाता है. इसमें यह जानकारी होती है, ताकि आपको यह पता चल सके कि क्या गड़बड़ी हुई है:

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