超时

所有 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/V25/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.SearchGoogleAdsService.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/V25/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;
        }
    }