超时
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
所有 Google Ads API 服务都有默认设置,包括供传输使用的超时时间。给定 Google Ads API 版本的任何服务都有一个专用 JSON 文件,其中定义了服务级和方法级的这些默认设置。例如,您可以点击此处查找与最新 Google Ads API 版本相关的文件。
默认设置足以满足大多数使用情形,但有时您可能需要替换这些设置。PHP 版客户端库支持替换服务器流式传输调用和一元调用的超时设置。
您可以将超时时间设置为 2 小时或更长时间,但 API 仍可能会使运行时间极长的请求超时,并返回 DEADLINE_EXCEEDED
错误。
替换服务器流式传输调用的超时时间
唯一使用此类调用的 Google Ads API 服务方法是 GoogleAdsService.SearchStream
。如需替换默认超时时间,您需要在调用该方法时添加一个额外的参数:
private static function makeServerStreamingCall(
GoogleAdsClient $googleAdsClient,
int $customerId
) {
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
// Creates a query that retrieves all campaign IDs.
$query = 'SELECT campaign.id FROM campaign';
$output = '';
try {
// Issues a search stream request by setting a custom client timeout.
/** @var GoogleAdsServerStreamDecorator $stream */
$stream = $googleAdsServiceClient->searchStream(
SearchGoogleAdsStreamRequest::build($customerId, $query),
[
// Any server streaming call has a default timeout setting. For this
// particular call, the default setting can be found in the following file:
// https://github.com/googleads/google-ads-php/blob/master/src/Google/Ads/GoogleAds/V21/Services/resources/google_ads_service_client_config.json.
//
// When making a server streaming call, an optional argument is provided and can
// be used to override the default timeout setting with a given value.
'timeoutMillis' => self::CLIENT_TIMEOUT_MILLIS
]
);
// Iterates over all rows in all messages and collects the campaign IDs.
foreach ($stream->iterateAllElements() as $googleAdsRow) {
/** @var GoogleAdsRow $googleAdsRow */
$output .= ' ' . $googleAdsRow->getCampaign()->getId();
}
print 'The server streaming call completed before the timeout.' . PHP_EOL;
} catch (ApiException $exception) {
if ($exception->getStatus() === ApiStatus::DEADLINE_EXCEEDED) {
print 'The server streaming call did not complete before the timeout.' . PHP_EOL;
} else {
// Bubbles up if the exception is not about timeout.
throw $exception;
}
} finally {
print 'All campaign IDs retrieved:' . ($output ?: ' None') . PHP_EOL;
}
}
替换一元调用的超时
大多数 Google Ads API 服务方法都使用一元调用;典型示例包括 GoogleAdsService.Search
和 GoogleAdsService.Mutate
。如需替换默认超时时间,您需要在调用该方法时添加一个额外的参数:
private static function makeUnaryCall(GoogleAdsClient $googleAdsClient, int $customerId)
{
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
// Creates a query that retrieves all campaign IDs.
$query = 'SELECT campaign.id FROM campaign';
$output = '';
try {
// Issues a search request by setting a custom client timeout.
$response = $googleAdsServiceClient->search(
SearchGoogleAdsRequest::build($customerId, $query),
[
// Any unary call is retryable and has default retry settings.
// Complete information about these settings can be found here:
// https://googleapis.github.io/gax-php/master/Google/ApiCore/RetrySettings.html.
// For this particular call, the default retry settings can be found in the
// following file:
// https://github.com/googleads/google-ads-php/blob/master/src/Google/Ads/GoogleAds/V21/Services/resources/google_ads_service_client_config.json.
//
// When making an unary call, an optional argument is provided and can be
// used to override the default retry settings with given values.
'retrySettings' => [
// Sets the maximum accumulative timeout of the call, it includes all tries.
'totalTimeoutMillis' => self::CLIENT_TIMEOUT_MILLIS,
// Sets the timeout that is used for the first try to one tenth of the
// maximum accumulative timeout of the call.
// Note: This overrides the default value and can lead to
// RequestError.RPC_DEADLINE_TOO_SHORT errors when too small. We recommend
// to do it only if necessary.
'initialRpcTimeoutMillis' => self::CLIENT_TIMEOUT_MILLIS / 10,
// Sets the maximum timeout that can be used for any given try to one fifth
// of the maximum accumulative timeout of the call (two times greater than
// the timeout that is used for the first try).
'maxRpcTimeoutMillis' => self::CLIENT_TIMEOUT_MILLIS / 5
]
]
);
// Iterates over all rows in all messages and collects the campaign IDs.
foreach ($response->iterateAllElements() as $googleAdsRow) {
/** @var GoogleAdsRow $googleAdsRow */
$output .= ' ' . $googleAdsRow->getCampaign()->getId();
}
print 'The unary call completed before the timeout.' . PHP_EOL;
} catch (ApiException $exception) {
if ($exception->getStatus() === ApiStatus::DEADLINE_EXCEEDED) {
print 'The unary call did not complete before the timeout.' . PHP_EOL;
} else {
// Bubbles up if the exception is not about timeout.
throw $exception;
}
} finally {
print 'All campaign IDs retrieved:' . ($output ?: ' None') . PHP_EOL;
}
}
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-27。
[null,null,["最后更新时间 (UTC):2025-08-27。"],[[["\u003cp\u003eAll Google Ads API services have default timeout settings, but they can be overridden if needed.\u003c/p\u003e\n"],["\u003cp\u003eServer streaming calls, like \u003ccode\u003eGoogleAdsService.SearchStream\u003c/code\u003e, allow overriding the default timeout with \u003ccode\u003etimeoutMillis\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eUnary calls, including \u003ccode\u003eGoogleAdsService.Search\u003c/code\u003e and \u003ccode\u003eGoogleAdsService.Mutate\u003c/code\u003e, allow overriding default retry settings, such as \u003ccode\u003etotalTimeoutMillis\u003c/code\u003e, to manage timeouts.\u003c/p\u003e\n"],["\u003cp\u003eWhile custom timeouts can be set to 2 hours or more, very long-running requests may still encounter \u003ccode\u003eDEADLINE_EXCEEDED\u003c/code\u003e errors from the API.\u003c/p\u003e\n"]]],[],null,["# Timeout\n\nAll Google Ads API services have default settings including timeouts that are used\nby the [transport](/google-ads/api/docs/client-libs/php/transport). Any service of a given Google Ads API version has a\ndedicated JSON file with these default settings defined at service and method\nlevels. For example, you can find the files related to the latest Google Ads API\nversion\n[here](https://github.com/googleads/google-ads-php/tree/HEAD/src/Google/Ads/GoogleAds/V21/Services/resources).\n\nThe default settings are adequate for most use cases, but there may be times\nwhen you need to override them. The client library for PHP supports overriding\ntimeout settings for both server streaming and unary calls.\n\nYou can set the timeout to 2 hours or more, but the API may still time out\nextremely long-running requests and return a\n[`DEADLINE_EXCEEDED`](/google-ads/api/reference/rpc/v21/InternalErrorEnum.InternalError) error.\n\nOverriding timeouts for a server streaming call\n-----------------------------------------------\n\nThe only Google Ads API service method that uses this type of call is\n[`GoogleAdsService.SearchStream`](/google-ads/api/reference/rpc/v21/GoogleAdsService/SearchStream).\nTo override the default timeout, you need to add an extra parameter when calling\nthe method:\n\n\n```php\nprivate static function makeServerStreamingCall(\n GoogleAdsClient $googleAdsClient,\n int $customerId\n) {\n $googleAdsServiceClient = $googleAdsClient-\u003egetGoogleAdsServiceClient();\n // Creates a query that retrieves all campaign IDs.\n $query = 'SELECT campaign.id FROM campaign';\n\n $output = '';\n try {\n // Issues a search stream request by setting a custom client timeout.\n /** @var GoogleAdsServerStreamDecorator $stream */\n $stream = $googleAdsServiceClient-\u003esearchStream(\n SearchGoogleAdsStreamRequest::build($customerId, $query),\n [\n // Any server streaming call has a default timeout setting. For this\n // particular call, the default setting can be found in the following file:\n // https://github.com/googleads/google-ads-php/blob/master/src/Google/Ads/GoogleAds/V21/Services/resources/google_ads_service_client_config.json.\n //\n // When making a server streaming call, an optional argument is provided and can\n // be used to override the default timeout setting with a given value.\n 'timeoutMillis' =\u003e self::CLIENT_TIMEOUT_MILLIS\n ]\n );\n // Iterates over all rows in all messages and collects the campaign IDs.\n foreach ($stream-\u003eiterateAllElements() as $googleAdsRow) {\n /** @var GoogleAdsRow $googleAdsRow */\n $output .= ' ' . $googleAdsRow-\u003egetCampaign()-\u003egetId();\n }\n print 'The server streaming call completed before the timeout.' . PHP_EOL;\n } catch (ApiException $exception) {\n if ($exception-\u003egetStatus() === ApiStatus::DEADLINE_EXCEEDED) {\n print 'The server streaming call did not complete before the timeout.' . PHP_EOL;\n } else {\n // Bubbles up if the exception is not about timeout.\n throw $exception;\n }\n } finally {\n print 'All campaign IDs retrieved:' . ($output ?: ' None') . PHP_EOL;\n }\n} \nhttps://github.com/googleads/google-ads-php/blob/be0249c30c27b4760387bec6682b82c9f4167761/examples/Misc/SetCustomClientTimeouts.php#L120-L160\n\n \n```\n\n\u003cbr /\u003e\n\nOverriding timeouts for a unary call\n------------------------------------\n\nMost of the Google Ads API service methods use unary calls; typical examples are\n[`GoogleAdsService.Search`](/google-ads/api/reference/rpc/v21/GoogleAdsService/Search) and\n[`GoogleAdsService.Mutate`](/google-ads/api/reference/rpc/v21/GoogleAdsService/Mutate).\nTo override the default timeout, you need to add an extra parameter when calling\nthe method:\n\n\n```php\nprivate static function makeUnaryCall(GoogleAdsClient $googleAdsClient, int $customerId)\n{\n $googleAdsServiceClient = $googleAdsClient-\u003egetGoogleAdsServiceClient();\n // Creates a query that retrieves all campaign IDs.\n $query = 'SELECT campaign.id FROM campaign';\n\n $output = '';\n try {\n // Issues a search request by setting a custom client timeout.\n $response = $googleAdsServiceClient-\u003esearch(\n SearchGoogleAdsRequest::build($customerId, $query),\n [\n // Any unary call is retryable and has default retry settings.\n // Complete information about these settings can be found here:\n // https://googleapis.github.io/gax-php/master/Google/ApiCore/RetrySettings.html.\n // For this particular call, the default retry settings can be found in the\n // following file:\n // https://github.com/googleads/google-ads-php/blob/master/src/Google/Ads/GoogleAds/V21/Services/resources/google_ads_service_client_config.json.\n //\n // When making an unary call, an optional argument is provided and can be\n // used to override the default retry settings with given values.\n 'retrySettings' =\u003e [\n // Sets the maximum accumulative timeout of the call, it includes all tries.\n 'totalTimeoutMillis' =\u003e self::CLIENT_TIMEOUT_MILLIS,\n // Sets the timeout that is used for the first try to one tenth of the\n // maximum accumulative timeout of the call.\n // Note: This overrides the default value and can lead to\n // RequestError.RPC_DEADLINE_TOO_SHORT errors when too small. We recommend\n // to do it only if necessary.\n 'initialRpcTimeoutMillis' =\u003e self::CLIENT_TIMEOUT_MILLIS / 10,\n // Sets the maximum timeout that can be used for any given try to one fifth\n // of the maximum accumulative timeout of the call (two times greater than\n // the timeout that is used for the first try).\n 'maxRpcTimeoutMillis' =\u003e self::CLIENT_TIMEOUT_MILLIS / 5\n ]\n ]\n );\n // Iterates over all rows in all messages and collects the campaign IDs.\n foreach ($response-\u003eiterateAllElements() as $googleAdsRow) {\n /** @var GoogleAdsRow $googleAdsRow */\n $output .= ' ' . $googleAdsRow-\u003egetCampaign()-\u003egetId();\n }\n print 'The unary call completed before the timeout.' . PHP_EOL;\n } catch (ApiException $exception) {\n if ($exception-\u003egetStatus() === ApiStatus::DEADLINE_EXCEEDED) {\n print 'The unary call did not complete before the timeout.' . PHP_EOL;\n } else {\n // Bubbles up if the exception is not about timeout.\n throw $exception;\n }\n } finally {\n print 'All campaign IDs retrieved:' . ($output ?: ' None') . PHP_EOL;\n }\n} \nhttps://github.com/googleads/google-ads-php/blob/be0249c30c27b4760387bec6682b82c9f4167761/examples/Misc/SetCustomClientTimeouts.php#L170-L223\n\n \n```\n\n\u003cbr /\u003e"]]