gpt4 book ai didi

php - 谷歌 PHP API : "invalid response from api server"

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:50:59 24 4
gpt4 key购买 nike

我在本地主机上的 Google App Engine 上运行的应用程序中使用 Google PHP API Client v2.0。当我将我的应用程序发布到 Google App Engine 产品时,我没有收到错误,但它是间歇性的,所以很难知道我是否只是在生产中走运。

我的 Google API 客户端正在尝试将文件写入我的 Google 云端硬盘。这段代码几个月来一直运行良好,但今天早上突然停止运行。我检查了 Google API 状态,他们没有报告任何中断。当我执行任何 API 调用时,我得到这个模糊的错误响应:

Fatal error: fopen(): Invalid response from API server. in /Users/me/googleappengine/stuff-otherstuff-111111/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php on line 312

该文件确实是在 Google Drive 上创建的,因此我的 API 调用可以通过,但是返回到我的脚本的任何响应都会导致致命的崩溃。

几分钟后它又开始工作了,现在又崩溃了。我真的无法在任何地方找到错误 API 服务器的无效响应 的任何解决方案......而且它是间歇性的。它似乎与我的代码无关,但 Google 表示它没有任何形式的中断。

我错过了什么?我该如何解决这个问题?

<?php
include_once('code-base.php');
require_once('vendor/autoload.php');

$client = new Google_Client();
$client->setApplicationName(getSetting('APP_NAME'));
$client->setAuthConfig(json_decode(getSetting('localhost_google_client_secret'), true));
$client->setAccessType("offline");
$client->setScopes(array(Google_Service_Drive::DRIVE));

$client->setHttpClient(new GuzzleHttp\Client(['verify'=>'ca-bundle.crt']));

$accesstoken = json_decode(getSetting('localhost_google_oauth_token'), true);

$client->setAccessToken($accesstoken);

// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
print "access token is expired\n";

$refreshtoken = getSetting('localhost_google_refresh_token');

print "refresh token: $refreshtoken\n";

$client->refreshToken($refreshtoken);
$newtokenjson = json_encode($client->getAccessToken());

print "new token: $newtokenjson\n";

printf("before: %s\n\nafter: %s\n\n", $accessToken, $newtokenjson);
//file_put_contents($credentialsPath, $newtokenjson);
updateSetting('localhost_google_oauth_token', $newtokenjson);
}

print "after checking access token expired\n";

print "before drive service\n";
$driveservice = new Google_Service_Drive($client);
print "after drive service\n";

$parentfolderid = getSetting('GDRIVE_EXPORT_DUMP');
$title = "00005 test gdrive.csv";
$filetype = 'text/csv';
$contents = "The quick brown fox jumped over the lazy dog.";

$file = new Google_Service_Drive_DriveFile();
$file->setName($title);
$file->setDescription($title);
$file->setMimeType($filetype);

$file->setParents(array($parentfolderid));

try {

if ($contents == null) {
print "contents of file are null, creating empty file\n";
$createdFile = $driveservice->files->create($file);
print "after creating empty file\n";

} else {
$contentsarray = array('data' => $contents, 'mimeType' => $filetype, 'uploadType' => 'media');
if ($options != null) {
foreach($options as $key => $value) {
$contentsarray[$key] = $value;
}
}
print "file contents: ".var_export($contentsarray, true)."\n";
$createdFile = $driveservice->files->create($file, $contentsarray);
print "after create file with contents\n";
}

return $createdFile;
} catch (Exception $e) {
print "EXCEPTION: An error occurred: " . $e->getMessage()."\n";
}

编辑:我的代码现在似乎一直失败,并显示 API 服务器的无效响应 消息。它只会在代码与 Google 通信的地方失败。这两个地方是在我调用 refreshToken() 时(这种情况很少发生),另一个是在我调用 create() 时。

我让我的 friend 在他的机器上使用相同的设置运行这段代码(据我们所知......相同的代码、相同的库、相同的 oauth token 等)并且它对他有效。

注意:getSetting() 函数上面的代码只是从我的数据库中提取一些字符串,我将这些字符串用于配置目的。此外,setHttpClient() 调用是必需的,因为它在运行在 PHP 5.5 上的 Google App Engine 中运行,PHP 5.5 有一个搞砸了的 CA Bundle,它需要我提供一个合适的。

什么可能导致我每次都失败,但为我的 friend 工作?

最佳答案

在回答“导致此错误的原因”之前,您首先需要回答“错误是什么”。通常,暂时性网络错误是正常的,不会迫使应用程序因 fatal error 而崩溃,即使它们经常发生(例如,在发生中断或在您的情况下,是某些本地问题)。

Guzzle 当然不会将错误视为 fatal error ,因为它 sets its default HTTP context to ignore_errors => true (并返回响应)。 google-api-php-client 不会更改 ignore_errors 的默认行为,google-api-php-client-services 也不会,也不是开发服务器,也不是 PHP SDK。文本来自 API 服务器的无效响应 没有出现在上述任何项目的源代码中的任何位置,也不是来自任何 API 的记录错误响应。

鉴于此,它表明 来自 API 服务器的无效响应 的来源在应用程序代码本身的某个地方。我的猜测是,在应用程序代码中有一种类似类型的包罗万象的异常处理 block ,就像您在上面看到的那样,它吞没了任何类型的非成功响应,并以不透明的错误消息作为 fatal error 。

我能给出的最佳答案是在您的代码中查找错误消息的来源,然后告诉它记录详细信息而不是掩盖它们,这可能会揭示潜在的问题是什么以及如何解决它。

关于php - 谷歌 PHP API : "invalid response from api server",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41151664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com