Google Ads API PHP 클라이언트 라이브러리의 src/Google/Ads/GoogleAds/vX
디렉터리(X는 Google Ads API 버전)에 있는 소스 코드는 게시된 proto 파일을 기반으로 GAPIC(생성된 API 클라이언트) 생성기를 사용하여 자동으로 생성됩니다.
그런 다음 생성된 소스 코드는 GoogleAdsClientBuilder::build()
를 호출하여 생성된 GoogleAdsClient
클래스를 사용하여 Google Ads API와 호환되는 서비스 클라이언트를 만드는 데 필요한 트레잇 및 클래스에 대한 참조를 포함하도록 수정됩니다. GoogleAdsClient
와 GoogleAdsClientBuilder
는 모두 src/Google/Ads/GoogleAds/Lib/vX/
에 있는 수동으로 생성된 클래스입니다.
생성된 클래스 위치
후처리된 서비스 클라이언트는 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()
가 필수 매개변수이므로 두 개의 매개변수($customerId
및 $query
)만 허용합니다. LIMIT
절을 무시하고 쿼리와 일치하는 총 결과 수를 요청하려면 setReturnTotalResultsCount()
를 사용하여 명시적으로 설정해야 합니다. 또는 패턴 3과 같이 모든 매개변수를 함께 SearchGoogleAdsRequest
의 생성자에 전달할 수 있습니다.
패턴 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]) ]) );