GAPIC

Google Ads API PHP 客户端库的 src/Google/Ads/GoogleAds/vX 目录(其中 X 是 Google Ads API 版本)中的源代码是根据已发布的 proto 文件,使用 GAPIC(生成的 API 客户端)生成器自动生成的。

然后,对生成的源代码进行修改,使其包含对 trait 和 创建与 Google Ads API 配合使用的服务客户端所需的类, GoogleAdsClient 类,该类通过调用 GoogleAdsClientBuilder::build()GoogleAdsClientGoogleAdsClientBuilder 是位于 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() 明确设置该值。或者,您也可以将所有参数一起传递给 SearchGoogleAdsRequest 的构造函数,如模式 3 所示。

模式 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])
    ])
);