逾時
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
所有 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;
}
}
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-27 (世界標準時間)。
[null,null,["上次更新時間: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"]]