GAPIC

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/ (예: V26)에 있고 해당 요청 및 응답 메시지 클래스 (예: 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: 유창한 setter

$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: 유창한 setter가 있는 정적 빌드 메서드

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

패턴 2: 유창한 setter

$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()가 생성되지 않습니다. setter 또는 배열 생성자를 사용하여 요청 객체를 구성해야 합니다.

패턴 1: 유창한 setter (권장)

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