क्लाइंट लाइब्रेरी का बुनियादी इस्तेमाल इस तरह किया जाता है:
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 इंस्टेंस बनाना
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, हर सर्विस क्लाइंट ऑब्जेक्ट के लिए एक 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",
$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
);
}