क्लाइंट लाइब्रेरी का बुनियादी इस्तेमाल इस तरह किया जाता है:
use Google\Ads\GoogleAds\Lib\V22\GoogleAdsClient;
use Google\Ads\GoogleAds\Lib\V22\GoogleAdsClientBuilder;
use Google\Ads\GoogleAds\Lib\V22\GoogleAdsException;
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;
use Google\ApiCore\ApiException;
// 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
$googleAdsClient = (new GoogleAdsClientBuilder())
->fromFile()
->withOAuth2Credential($oAuth2Credential)
->withLoginCustomerId(1234567890) // Replace 1234567890 with your login customer ID.
->build();
// Create the CampaignServiceClient.
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
// Make calls to CampaignServiceClient.
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())
->fromFile()
->build();
// Construct a Google Ads client configured from a properties file
$googleAdsClient = (new GoogleAdsClientBuilder())
->fromFile()
->withOAuth2Credential($oAuth2Credential)
->withLoginCustomerId(1234567890) // Replace 1234567890 with your login customer ID.
->build();
कोई सेवा बनाना
GoogleAdsClient हर सेवा क्लाइंट ऑब्जेक्ट के लिए, गेटर मेथड उपलब्ध कराता है.
उदाहरण के लिए, CampaignServiceClient का इंस्टेंस बनाने के लिए, GoogleAdsClient->getCampaignServiceClient() मेथड को कॉल करें. जैसा कि पिछले उदाहरण में दिखाया गया है.
गड़बड़ी ठीक करना
यह ज़रूरी नहीं है कि हर एपीआई कॉल पूरा हो. अगर किसी वजह से आपके एपीआई कॉल पूरे नहीं हो पाते, तो सर्वर गड़बड़ियां दिखा सकता है. एपीआई की गड़बड़ियों को कैप्चर करना और उन्हें सही तरीके से ठीक करना ज़रूरी है.
एपीआई से जुड़ी गड़बड़ी होने पर, GoogleAdsException इंस्टेंस थ्रो किया जाता है. इसमें गड़बड़ी के बारे में जानकारी होती है, ताकि आपको यह पता चल सके कि क्या गड़बड़ी हुई है:
use Google\Ads\GoogleAds\Lib\V22\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
);
}