Google Ads API PHP クライアント ライブラリの src/Google/Ads/GoogleAds/vX
ディレクトリにあるソースコード(X は Google Ads API のバージョン)は、公開されている proto ファイルに基づいて、GAPIC(Generated API Client)Generator を使用して自動的に生成されます。
生成されたソースコードは変更され、GoogleAdsClientBuilder::build()
を呼び出して作成された GoogleAdsClient
クラスを使用して、Google Ads API と連携するサービス クライアントを作成するために必要なトレイトとクラスへの参照が含まれるようになります。GoogleAdsClient
と GoogleAdsClientBuilder
はどちらも、src/Google/Ads/GoogleAds/Lib/vX/
にある手動で作成されたクラスです。
GAPIC v2 ソースコード
バージョン v20.1.0 以降のクライアント ライブラリでは、リクエスト オブジェクトをサービス クライアントのメソッドに渡すことをサポートする、新しいバージョンの GAPIC ソースコードも src/Google/Ads/GoogleAds/vX
に含まれています。GAPIC v2 と呼ばれるこの新しいバージョンは、今後の新機能の準備にもなります。以前の GAPIC ソースコードである GAPIC v1 は引き続き生成され、2023 年末まで各リリースに含まれています。
Google Ads API PHP クライアント ライブラリでは、構成設定 useGapicV2Source
を使用して、GoogleAdsClient
にリンクするバージョンを選択できます。この設定を true
に設定すると、GAPIC v2 サービス クライアントを作成する GoogleAdsClient
オブジェクトがクライアント ライブラリによって生成されます。
生成されたクラスの場所
2 つのクラスタイプの GAPIC バージョン間でのファイルの場所の違いを以下に示します。
GAPIC で生成されたクライアントと関連ファイル | GAPIC v1
src/Google/Ads/GoogleAds/VX/Services/Gapic/GAPIC v2: なし。後処理されたファイルのみが生成されます。 |
後処理されたクライアント | GAPIC v1
src/Google/Ads/GoogleAds/VX/Services/GAPIC v2 src/Google/Ads/GoogleAds/VX/Services/Client/ |
用途
GAPIC v1 では、各リクエスト パラメータをメソッドに直接渡す必要がありますが、GAPIC v2 では、代わりにリクエスト オブジェクトを渡す必要があります。場合によっては、リクエスト オブジェクトを作成する方法が複数あることがあります。これは、GAPIC v2 が必須パラメータを渡すための便利なメソッド build()
も生成しているためです。
例 1.1: 必須パラメータを含むメソッド
次のサンプルコードは、GAPIC v1 と v2 での CampaignService::mutate()
の呼び出しを比較したものです。すべてのパラメータ($customerId
と $operations
)が必須パラメータであるため、両方のパラメータを受け入れる build()
は GAPIC v2 コードで生成されます。
パターン 1 | GAPIC v1
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient(); $response = $campaignServiceClient->mutateCampaigns( $customerId, $campaignOperations );GAPIC v2 $campaignServiceClient = $googleAdsClient->getCampaignServiceClient(); $response = $campaignServiceClient->mutateCampaigns( MutateCampaignsRequest::build( $customerId, $campaignOperations ) ); |
パターン 2 | GAPIC v1
N/AGAPIC v2 $campaignServiceClient = $googleAdsClient->getCampaignServiceClient(); $request = (new MutateCampaignsRequest()) ->setCustomerId($customerId) ->setCampaignOperations($campaignOperations); $response = $campaignServiceClient->mutateCampaigns($request); |
例 1.2: 必須パラメータとオプション パラメータを持つメソッド
次のサンプルコードは、GAPIC v1 と v2 での GoogleAdsServiceClient::search()
の呼び出しを比較したものです。この例では、GAPIC v2 ソースコードで生成される build()
は、必須パラメータである 2 つのパラメータ($customerId
と $query
)のみを受け入れます。ページサイズ(オプションのパラメータ)を設定するには、setPageSize()
を使用して明示的に設定する必要があります。または、パターン 3 に示すように、すべてのパラメータを SearchGoogleAdsRequest
のコンストラクタに渡すこともできます。
パターン 1 | GAPIC v1
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); $response = $googleAdsServiceClient->search( $customerId, $query, ['pageSize' => self::PAGE_SIZE] );GAPIC v2 $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); $response = $googleAdsServiceClient->search( SearchGoogleAdsRequest::build($customerId, $query) ->setPageSize(self::PAGE_SIZE) ); |
パターン 2 | GAPIC v1
N/AGAPIC v2 $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); $request = (new SearchGoogleAdsRequest()) ->setCustomerId($customerId) ->setQuery($query) ->setPageSize(self::PAGE_SIZE); $response = $googleAdsServiceClient->search($request); |
パターン 3 | GAPIC v1
N/AGAPIC v2 $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); $request = (new SearchGoogleAdsRequest([ 'customer_id' => $customerId, 'query' => $query, 'page_size' => self::PAGE_SIZE ]); $response = $googleAdsServiceClient->search($request); |
例 2: オプション パラメータのみを持つメソッド
GAPIC v1 と v2 での GeoTargetConstantServiceClient::suggestGeoTargetConstants()
の呼び出しを比較します。GeoTargetConstantServiceClient::suggestGeoTargetConstants()
のパラメータはすべて省略可能であるため、この場合、build()
は GAPIC v2 ソースコードでは生成されません。リクエスト オブジェクトは自分で作成する必要があります。
パターン 1 | GAPIC v1
$geoTargetConstantServiceClient = $googleAdsClient->getGeoTargetConstantServiceClient(); $response = $geoTargetConstantServiceClient->suggestGeoTargetConstants([ 'locale' => $locale, 'countryCode' => $countryCode, 'locationNames' => new LocationNames(['names' => $locationNames]) ]);GAPIC v2 $geoTargetConstantServiceClient = $googleAdsClient->getGeoTargetConstantServiceClient(); $request = (new SuggestGeoTargetConstantsRequest()) ->setLocale($locale) ->setCountryCode($countryCode) ->setLocationNames(new LocationNames(['names' => $locationNames])); $response = $geoTargetConstantServiceClient->suggestGeoTargetConstants($request); |
パターン 2 | GAPIC v1
N/AGAPIC v2 $geoTargetConstantServiceClient = $googleAdsClient->getGeoTargetConstantServiceClient(); $response = $geoTargetConstantServiceClient->suggestGeoTargetConstants( new SuggestGeoTargetConstantsRequest([ 'locale' => $locale, 'country_code' => $countryCode, 'location_names' => new LocationNames(['names' => $locationNames]) ]) ); |