Google Ads API PHP クライアント ライブラリの src/Google/Ads/GoogleAds/vX
ディレクトリにあるソースコード(X は Google Ads API のバージョン)は、公開された proto ファイルに基づいて、GAPIC(生成された API クライアント)ジェネレータを使用して自動的に生成されます。
生成されたソースコードを変更して、GoogleAdsClientBuilder::build()
を呼び出して作成される GoogleAdsClient
クラスを使用して Google 広告 API と連携するサービス クライアントの作成に必要なトレイトとクラスへの参照を含めます。GoogleAdsClient
と GoogleAdsClientBuilder
はどちらも、src/Google/Ads/GoogleAds/Lib/vX/
にある手動で作成されたクラスです。
生成されたクラスの場所
後処理されたサービス クライアントは src/Google/Ads/GoogleAds/VX/Services/Client/
にあります。
用途
リクエスト オブジェクトを作成して、使用するクライアントに渡す必要があります。クライアントによっては、必須パラメータを渡すための便利な build()
というメソッドも用意されているため、リクエスト オブジェクトを作成するには複数の方法があります。
例 1.1: 必須パラメータを含むメソッド
次のサンプルコードは、CampaignService::mutate()
を呼び出す方法を示しています。すべてのパラメータ($customerId
と $operations
)が必須パラメータであるため、両方のパラメータを受け入れる build()
がコードで生成されます。
パターン 1
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient(); $response = $campaignServiceClient->mutateCampaigns( MutateCampaignsRequest::build( $customerId, $campaignOperations ) );
パターン 2
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient(); $request = (new MutateCampaignsRequest()) ->setCustomerId($customerId) ->setCampaignOperations($campaignOperations); $response = $campaignServiceClient->mutateCampaigns($request);
例 1.2: 必須パラメータとオプション パラメータを含むメソッド
次のサンプルコードは GoogleAdsServiceClient::search()
を呼び出します。この例では、コードで生成される build()
は、必須パラメータであるため、2 つのパラメータ($customerId
と $query
)のみを受け入れます。LIMIT
句を無視してクエリに一致する結果の合計数をリクエストするには、setReturnTotalResultsCount()
を使用して明示的に設定する必要があります。または、パターン 3 に示すように、すべてのパラメータを SearchGoogleAdsRequest
のコンストラクタに一緒に渡すこともできます。
パターン 1
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); $response = $googleAdsServiceClient->search( SearchGoogleAdsRequest::build($customerId, $query) ->setReturnTotalResultsCount(true) );
パターン 2
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); $request = (new SearchGoogleAdsRequest()) ->setCustomerId($customerId) ->setQuery($query) ->setReturnTotalResultsCount(true); $response = $googleAdsServiceClient->search($request);
パターン 3
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); $request = (new SearchGoogleAdsRequest([ 'customer_id' => $customerId, 'query' => $query, 'return_total_results_count' => true ]); $response = $googleAdsServiceClient->search($request);
例 2: 省略可能なパラメータのみを含むメソッド
この例は、GeoTargetConstantServiceClient::suggestGeoTargetConstants()
を呼び出す方法を示しています。GeoTargetConstantServiceClient::suggestGeoTargetConstants()
のすべてのパラメータは省略可能であるため、この場合、build()
はソースコードで生成されません。リクエスト オブジェクトは自分で作成する必要があります。
パターン 1
$geoTargetConstantServiceClient = $googleAdsClient->getGeoTargetConstantServiceClient(); $request = (new SuggestGeoTargetConstantsRequest()) ->setLocale($locale) ->setCountryCode($countryCode) ->setLocationNames(new LocationNames(['names' => $locationNames])); $response = $geoTargetConstantServiceClient->suggestGeoTargetConstants($request);
パターン 2
$geoTargetConstantServiceClient = $googleAdsClient->getGeoTargetConstantServiceClient(); $response = $geoTargetConstantServiceClient->suggestGeoTargetConstants( new SuggestGeoTargetConstantsRequest([ 'locale' => $locale, 'country_code' => $countryCode, 'location_names' => new LocationNames(['names' => $locationNames]) ]) );