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/。
生成されたクラスの場所
後処理済みのサービス クライアントは、Kubernetes Engine の
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() を使用して明示的に設定する必要があります。または
すべてのパラメータを一緒に
SearchGoogleAdsRequest(パターン 3 を参照)。
パターン 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]) ]) );