Google API

Google Ads API PHP クライアント ライブラリの src/Google/Ads/GoogleAds/vX ディレクトリ(X は Google Ads API のバージョン)にあるソースコードは、公開されている proto ファイルに基づき、GAPIC(生成された API クライアント)生成ツールを使って自動的に生成されます。

生成されたソースコードは、Google Ads API と連携するサービス クライアントの作成に必要なトレイトとクラスへの参照が含まれるように変更されます(GoogleAdsClient クラスは GoogleAdsClientBuilder::build() を呼び出して作成します)。GoogleAdsClientGoogleAdsClientBuilder はどちらも、src/Google/Ads/GoogleAds/Lib/vX/ に手動で作成されたクラスです。

GAPIC v2 ソースコード

バージョン v20.1.0 以降、クライアント ライブラリの src/Google/Ads/GoogleAds/vX には新しいバージョンの GAPIC ソースコードも含まれています。これにより、サービス クライアントのメソッドにリクエスト オブジェクトを渡すことができます。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/A
GAPIC 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/A
GAPIC v2
$googleAdsServiceClient
    = $googleAdsClient->getGoogleAdsServiceClient();
$request = (new SearchGoogleAdsRequest())
    ->setCustomerId($customerId)
    ->setQuery($query)
    ->setPageSize(self::PAGE_SIZE);
$response = $googleAdsServiceClient->search($request);
      
パターン 3 GAPIC v1
N/A
GAPIC 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/A
GAPIC v2
$geoTargetConstantServiceClient =
    $googleAdsClient->getGeoTargetConstantServiceClient();
$response = $geoTargetConstantServiceClient->suggestGeoTargetConstants(
    new SuggestGeoTargetConstantsRequest([
        'locale' => $locale,
        'country_code' => $countryCode,
        'location_names' => new LocationNames(['names' => $locationNames])
    ])
);