我已使用适用于PHP的Drive SDK "google/apiclient": "^2.0"
将网站备份上传到google驱动器,就上传而言,它工作非常完美,现在我正尝试使用相同的SDK从驱动器下载备份的文件,但是问题是,在某些服务器上它可以完美运行,而在其他服务器上,它可以给我Header already sent issue
。我将其作为WordPress的插件,并包含用于下载这些文件的标题,我知道这不是一个好主意,但是应该替代它。通过我正在下载这些文件的代码如下。
$tempID = Helper::getBackupById($backupID);
$AT = Helper::get_Settings('google_token');
$refreshToken = Helper::get_Settings('google_refresh_token');
$client = new Google_Client();
$client->setClientId('CLIENT ID');
$client->setClientSecret('CLIENT_SECRET');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$client->setAccessToken($AT);
if ($client->isAccessTokenExpired()) {
$request = $client->refreshToken($refreshToken);
$newAT = $request['access_token'];
$newRefreshToken = $request['refresh_token'];
Helper::save_or_update_settings('google_token', $newAT);
Helper::save_or_update_settings('google_refresh_token', $newRefreshToken);
$client->setAccessToken($newAT);
}
$service = new Google_Service_Drive($client);
$google_keys = unserialize($tempID[0]->google_keys);
$parentId = $google_keys['g_folder'];
$lists = $service->files->listFiles(array('q' => "'$parentId' in parents"));
foreach ($lists as $list) {
if (!is_dir(wp_upload_dir()['basedir'] . '/Restore')) {
@mkdir(wp_upload_dir()['basedir'] . '/Restore', 0777);
}
$storeIn = wp_upload_dir()['basedir'] . '/Restore/' . $list->name . '.zip';
$content = $service->files->get($list->id, array('alt' => 'media'));
$headers = $content->getHeaders();
foreach ($headers as $name => $values) {
header($name . ': ' . implode(', ', $values));
}
header('Content-Disposition: attachment; filename="' . $tempID[0]->file . '"');
$body = $content->getBody();
while (!$body->eof()) {
file_put_contents($storeIn, $body);
}
echo "Done.\n";
}
现在,我的问题是,是否有其他下载方式来下载这些文件而不涉及任何标头。如果是的话,只是提示会很有帮助。
答案 0 :(得分:1)
我只是尝试了一下,但效果很好,这段代码中的标题部分没有用,我刚刚删除了该代码,如果将来他需要它,它对某些人来说也很好,这是更新的代码。
$tempID = Helper::getBackupById($backupID);
$AT = Helper::get_Settings('google_token');
$refreshToken = Helper::get_Settings('google_refresh_token');
$client = new Google_Client();
$client->setClientId('CLIENT ID');
$client->setClientSecret('CLIENT_SECRET');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$client->setAccessToken($AT);
if ($client->isAccessTokenExpired()) {
$request = $client->refreshToken($refreshToken);
$newAT = $request['access_token'];
$newRefreshToken = $request['refresh_token'];
Helper::save_or_update_settings('google_token', $newAT);
Helper::save_or_update_settings('google_refresh_token', $newRefreshToken);
$client->setAccessToken($newAT);
}
$service = new Google_Service_Drive($client);
$google_keys = unserialize($tempID[0]->google_keys);
$parentId = $google_keys['g_folder'];
$lists = $service->files->listFiles(array('q' => "'$parentId' in parents"));
foreach ($lists as $list) {
if (!is_dir(wp_upload_dir()['basedir'] . '/Restore')) {
@mkdir(wp_upload_dir()['basedir'] . '/Restore', 0777);
}
$storeIn = wp_upload_dir()['basedir'] . '/Restore/' . $list->name . '.zip';
$content = $service->files->get($list->id, array('alt' => 'media'));
$body = $content->getBody();
while (!$body->eof()) {
file_put_contents($storeIn, $body);
}
echo "Done.\n";
}