Mã nguồn trong src/Google/Ads/GoogleAds/vX
thư mục thư viện ứng dụng PHP API Google Ads, trong đó X là API Google Ads
được tạo tự động bằng GAPIC (Ứng dụng API đã tạo)
Trình tạo, dựa trên đã phát hành
proto
.
Sau đó, mã nguồn được tạo sẽ được sửa đổi để chứa các tệp tham chiếu đến các đặc điểm và lớp cần thiết để tạo ứng dụng dịch vụ hoạt động với API Google Ads bằng cách sử dụng lớp GoogleAdsClient được tạo bằng cách gọi GoogleAdsClientBuilder::build(). Cả GoogleAdsClient và
GoogleAdsClientBuilder là các lớp học được tạo theo cách thủ công nằm trong
src/Google/Ads/GoogleAds/Lib/vX/.
Vị trí lớp đã tạo
Khách hàng dịch vụ sau được xử lý nằm ở
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
Mã mẫu sau đây cho biết cách gọi CampaignService::mutate(). Tất 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ố 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 hai tham số ($customerId và $query) vì đó 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
bạn phải đặt giá trị này một cách rõ ràng bằng setReturnTotalResultsCount(). Ngoài ra, bạn có thể truyền tất cả tham số cùng nhau đến hàm khởi tạo của SearchGoogleAdsRequest, như 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: Các phương thức chỉ 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 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]) ]) );