X이 Google Ads API 버전 번호인 Google Ads API PHP 클라이언트 라이브러리의 src/Google/Ads/GoogleAds/VX 디렉터리에 있는 소스 코드는 게시된 proto 파일을 기반으로 GAPIC (생성된 API 클라이언트) 생성기를 사용하여 자동으로 생성됩니다.
그런 다음 생성된 소스 코드를 수정하여 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() 팩토리 메서드를 비롯한 여러 방법으로 요청 객체를 만들 수 있습니다.
예 1.1: 필수 매개변수가 있는 메서드
다음 샘플 코드에서는 CampaignServiceClient::mutateCampaigns()를 호출하는 방법을 보여줍니다. $customerId와 $campaignOperations는 모두 필수 매개변수이므로 두 매개변수를 모두 허용하는 정적 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])
])
);