GAPIC

X は Google Ads API のバージョン番号です。Google Ads API PHP クライアント ライブラリの src/Google/Ads/GoogleAds/VX ディレクトリにあるソースコードは、公開された proto ファイルに基づいて、GAPIC(Generated API Client)ジェネレータを使用して自動的に生成されます。

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

生成されたクラスの場所

後処理されたサービス クライアントは src/Google/Ads/GoogleAds/VX/Services/Client/(V25 など)にあり、対応するリクエストとレスポンスのメッセージ クラス(MutateCampaignsRequest や SearchGoogleAdsRequest など)は src/Google/Ads/GoogleAds/VX/Services/ にあります。マイナー API リリース(v25.1 など)は、メジャー バージョン ディレクトリ(V25)内に生成されます。

用途

リクエスト オブジェクトを作成し、使用するサービスクライアントに渡す必要があります。パラメータが必須か省略可能かによって、リクエスト オブジェクトを作成する方法は複数あります。たとえば、必須パラメータを渡すための静的 build() Factory メソッドなどがあります。

例 1.1: 必須パラメータを持つメソッド

次のサンプルコードは、CampaignServiceClient::mutateCampaigns() を呼び出す方法を示しています。$customerId と $campaignOperations はどちらも必須パラメータであるため、両方のパラメータを受け取る静的 build() ファクトリ メソッドがリクエスト クラスで生成されます。

パターン 1: 静的ビルド方法(推奨)

$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns(
    MutateCampaignsRequest::build(
        $customerId,
        $campaignOperations
    )
);

パターン 2: Fluent セッター

$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$request = (new MutateCampaignsRequest())
    ->setCustomerId($customerId)
    ->setCampaignOperations($campaignOperations);
$response = $campaignServiceClient->mutateCampaigns($request);

例 1.2: 必須パラメータと省略可能なパラメータを含むメソッド

次のサンプルコードは GoogleAdsServiceClient::search() を呼び出します。この例では、生成された build() メソッドは、必須パラメータである $customerId と $query の 2 つのパラメータのみを受け取ります。LIMIT 句を無視してクエリに一致する結果の合計数をリクエストするには、setReturnTotalResultsCount() を使用して明示的に設定します。または、パターン 3 に示すように、連想配列ですべてのパラメータをまとめて SearchGoogleAdsRequest のコンストラクタに渡すこともできます。

パターン 1: 流暢なセッターを使用した静的ビルドメソッド

$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
$response = $googleAdsServiceClient->search(
    SearchGoogleAdsRequest::build($customerId, $query)
        ->setReturnTotalResultsCount(true)
);

パターン 2: Fluent セッター

$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: Fluent セッター(推奨)

$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])
    ])
);