Am使用php google电子表格客户端将一些数据写入电子表格。这花了一年左右的时间,但是 突然,它停止工作并开始引发错误。调试后,我发现它显示了一些与 reCAPTCHA。下面的屏幕快照将向您显示外观。
我的过程是一个cronjob,它将每分钟执行一次。在此过程中,电子表格中发生了大量读写操作。 我显然不明白为什么会抛出此reCAPTCHA错误以及如何防止它。这从未发生过。 请让我知道你的想法。我的示例代码如下:
<?php
ob_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
set_time_limit(0);
require_once 'google-api-php-client/autoload.php';
use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServiceRequestFactory;
use Google\Spreadsheet\SpreadsheetService;
const CLIENT_APP_NAME = 'spreadsheetdemo';
const CLIENT_ID = 'a working id';
const CLIENT_EMAIL = 'a working email';
const CLIENT_KEY_PATH = 'a working p12 key file';
const CLIENT_KEY_PW = 'notasecret';
$objClientAuth = new Google_Client();
$objClientAuth->setApplicationName(CLIENT_APP_NAME);
$objClientAuth->setClientId(CLIENT_ID);
$objClientAuth->setAssertionCredentials(new Google_Auth_AssertionCredentials(
CLIENT_EMAIL,
array('https://spreadsheets.google.com/feeds', 'https://docs.google.com/feeds'),
file_get_contents(dirname(__FILE__) . '/' . CLIENT_KEY_PATH),
CLIENT_KEY_PW
));
$objClientAuth->getAuth()->refreshTokenWithAssertion();
$objToken = json_decode($objClientAuth->getAccessToken());
$accessToken = $objToken->access_token;
$serviceRequest = new DefaultServiceRequest($accessToken);
ServiceRequestFactory::setInstance($serviceRequest);
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetTitle = 'AwesomeSpreadsheet';
$spreadsheetFeed = $spreadsheetService->getSpreadsheetFeed(); // Error happens at this line.
$spreadsheet = $spreadsheetFeed->getByTitle($spreadsheetTitle);
?>
我使用try catch方法得到了屏幕截图错误
try {
$spreadsheetFeed = $spreadsheetService->getSpreadsheetFeed();
} catch (\Exception $e) {
print_r($e->getMessage());
}
否则在error_log中,它将显示以下消息-
“ PHP致命错误:未捕获的异常 'Google \ Spreadsheet \ Exception \ BadRequestException“