Mã nguồn trong thư mục src/Google/Ads/GoogleAds/vX
của thư viện ứng dụng Google Ads API PHP (trong đó X là phiên bản Google Ads API) được tạo tự động bằng Trình tạo GAPIC (Ứng dụng API được tạo), dựa trên các tệp proto đã xuất bản.
Sau đó, mã nguồn đã tạo sẽ được sửa đổi để chứa các tham chiếu đến những đặc điểm và lớp cần thiết để tạo ứng dụng khách dịch vụ hoạt động với Google Ads API bằng lớp GoogleAdsClient
. Lớp này được tạo bằng cách gọi GoogleAdsClientBuilder::build()
. Cả GoogleAdsClient
và GoogleAdsClientBuilder
đều là các lớp được tạo theo cách thủ công nằm trong src/Google/Ads/GoogleAds/Lib/vX/
.
Vị trí lớp học được tạo
Các ứng dụng dịch vụ đã xử lý hậu kỳ nằm trong src/Google/Ads/GoogleAds/VX/Services/Client/
.
Cách sử dụng
Bạn phải tạo một đối tượng yêu cầu và truyền đối tượng đó đến ứng dụng mà bạn muốn sử dụng.
Trong một số trường hợp, bạn có nhiều cách để tạo đối tượng yêu cầu vì một số ứng dụng cũng có một phương thức thuận tiện có tên là build()
để truyền các tham số bắt buộc.
Ví dụ 1.1: Phương thức có tham số bắt buộc
Đoạn mã mẫu sau đây cho biết cách gọi CampaignService::mutate()
. Tất cả các tham số ($customerId
và $operations
) đều là tham số bắt buộc, vì vậy, build()
chấp nhận cả hai tham số này sẽ được tạo trong mã.
Mẫu 1
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient(); $response = $campaignServiceClient->mutateCampaigns( MutateCampaignsRequest::build( $customerId, $campaignOperations ) );
Mẫu 2
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient(); $request = (new MutateCampaignsRequest()) ->setCustomerId($customerId) ->setCampaignOperations($campaignOperations); $response = $campaignServiceClient->mutateCampaigns($request);
Ví dụ 1.2: Phương thức có tham số bắt buộc và tham số không bắt buộc
Mã mẫu sau đây gọi GoogleAdsServiceClient::search()
. Trong ví dụ này, build()
được tạo trong mã chỉ chấp nhận 2 tham số ($customerId
và $query
) vì đây là các tham số bắt buộc. Để yêu cầu tổng số kết quả khớp với truy vấn bỏ qua mệnh đề LIMIT
, bạn phải đặt một cách rõ ràng bằng cách sử dụng setReturnTotalResultsCount()
. Ngoài ra, bạn có thể truyền tất cả các tham số cùng nhau đến hàm khởi tạo của SearchGoogleAdsRequest
, như minh hoạ trong mẫu 3.
Mẫu 1
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); $response = $googleAdsServiceClient->search( SearchGoogleAdsRequest::build($customerId, $query) ->setReturnTotalResultsCount(true) );
Mẫu 2
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); $request = (new SearchGoogleAdsRequest()) ->setCustomerId($customerId) ->setQuery($query) ->setReturnTotalResultsCount(true); $response = $googleAdsServiceClient->search($request);
Mẫu 3
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); $request = (new SearchGoogleAdsRequest([ 'customer_id' => $customerId, 'query' => $query, 'return_total_results_count' => true ]); $response = $googleAdsServiceClient->search($request);
Ví dụ 2: Phương thức chỉ có các tham số không bắt buộc
Ví dụ này cho thấy cách gọi GeoTargetConstantServiceClient::suggestGeoTargetConstants()
. Vì tất cả các tham số của GeoTargetConstantServiceClient::suggestGeoTargetConstants()
đều là tham số không bắt buộc, nên build()
không được tạo trong mã nguồn trong trường hợp này – bạn phải tự tạo đối tượng yêu cầu.
Mẫu 1
$geoTargetConstantServiceClient = $googleAdsClient->getGeoTargetConstantServiceClient(); $request = (new SuggestGeoTargetConstantsRequest()) ->setLocale($locale) ->setCountryCode($countryCode) ->setLocationNames(new LocationNames(['names' => $locationNames])); $response = $geoTargetConstantServiceClient->suggestGeoTargetConstants($request);
Mẫu 2
$geoTargetConstantServiceClient = $googleAdsClient->getGeoTargetConstantServiceClient(); $response = $geoTargetConstantServiceClient->suggestGeoTargetConstants( new SuggestGeoTargetConstantsRequest([ 'locale' => $locale, 'country_code' => $countryCode, 'location_names' => new LocationNames(['names' => $locationNames]) ]) );