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 src/Google/Ads/GoogleAds/VX/Services/Client/BaseClient/ |
後処理されたクライアント | 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()
のパラメータはすべて省略可能であるため、この場合、GAPIC v2 のソースコードでは build()
は生成されません。リクエスト オブジェクトを自分で作成する必要があります。
パターン 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]) ]) ); |