Waktu habis
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Semua layanan Google Ads API memiliki setelan default, termasuk waktu tunggu yang digunakan oleh transport. Setiap layanan dari versi Google Ads API tertentu memiliki
file JSON khusus dengan setelan default ini yang ditentukan di tingkat layanan dan metode. Misalnya, Anda dapat menemukan file yang terkait dengan versi terbaru Google Ads API di sini.
Setelan default sudah memadai untuk sebagian besar kasus penggunaan, tetapi mungkin ada saatnya Anda perlu menggantinya. Library klien untuk PHP mendukung penggantian
setelan waktu tunggu untuk panggilan streaming server dan unary.
Anda dapat menyetel waktu tunggu hingga 2 jam atau lebih, tetapi API mungkin masih mengalami waktu tunggu untuk permintaan yang berjalan sangat lama dan menampilkan error DEADLINE_EXCEEDED
.
Mengganti waktu tunggu untuk panggilan streaming server
Satu-satunya metode layanan Google Ads API yang menggunakan jenis panggilan ini adalah
GoogleAdsService.SearchStream
.
Untuk mengganti waktu tunggu default, Anda perlu menambahkan parameter tambahan saat memanggil
metode:
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;
}
}
Mengganti waktu tunggu untuk panggilan unary
Sebagian besar metode layanan Google Ads API menggunakan panggilan unary; contoh umumnya adalah
GoogleAdsService.Search
dan
GoogleAdsService.Mutate
.
Untuk mengganti waktu tunggu default, Anda perlu menambahkan parameter tambahan saat memanggil
metode:
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;
}
}
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-08-27 UTC.
[null,null,["Terakhir diperbarui pada 2025-08-27 UTC."],[[["\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"]]