บริการทั้งหมดของ Google Ads API มีการตั้งค่าเริ่มต้น รวมถึงการหมดเวลาที่ใช้โดยการรับส่ง บริการใดก็ตามของ Google Ads API เวอร์ชันที่กำหนดจะมีไฟล์ JSON เฉพาะที่มีการกำหนดการตั้งค่าเริ่มต้นเหล่านี้ที่ระดับบริการและเมธอด เช่น คุณดูไฟล์ที่เกี่ยวข้องกับ Google Ads API เวอร์ชัน ล่าสุดได้ที่นี่
การตั้งค่าเริ่มต้นเพียงพอสำหรับกรณีการใช้งานส่วนใหญ่ แต่บางครั้งคุณอาจต้องลบล้างการตั้งค่าเหล่านั้น ไลบรารีของไคลเอ็นต์สำหรับ PHP รองรับการลบล้าง การตั้งค่าการหมดเวลาสำหรับการสตรีมฝั่งเซิร์ฟเวอร์และการเรียกแบบเอกภาคทั้ง 2 แบบ
คุณตั้งค่าการหมดเวลาเป็น 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/V20/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/V20/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; } }