Google Drive API httpRequest下载文件对话框
问题描述:
我是PHP新手,我一直在使用Google Drive PHP API。Google Drive API httpRequest下载文件对话框
这是我的代码:
function downloadFile1($service,$downloadUrl,$client) {
if ($downloadUrl) {
$request = new Google_Http_Request($downloadUrl, 'GET', null, null);
$SignhttpRequest = $client->getAuth()->sign($request);
$httpRequest = $client->getIo()->makeRequest($SignhttpRequest);
if ($httpRequest->getResponseHttpCode() == 200) {
return $httpRequest->getResponseBody();
} else {
// An error occurred.
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}
到目前为止好。但我想获得一个对话框来保存文件到我的电脑(当我得到返回结果)。我怎么能得到这个?
[如果我执行
$content = $httpRequest->getResponseBody();
print_r($content);]
我可以看到在网页文件的内容,但我想下载吧!]
提前感谢!
答
嗨所有也许这个代码将帮助人们使用谷歌驱动器作为一种服务,可以用于与cronjob/cli上传,删除和下载无限文件大小的文件。
它尚未完成,但它的谷歌apiclient包装。 并可用于上传,下载和删除文件。
<?php
namespace App\Model\Google;
class Drive
{
public $pixie;
protected $_config;
protected $_client;
protected $_service;
public function __construct(\App\Pixie $pixie)
{
$this->pixie = $pixie;
$this->_config = $this->pixie->config->get('googledrive');
$this->_client = new \Google_Client();
$this->_client->setClientId($this->_config['clientId']);
$this->_client->setClientSecret($this->_config['clientSecret']);
$this->_client->setRedirectUri($this->_config['redirectUri']); //
$this->_client->addScope($this->_config['scope']); // scope: https://www.googleapis.com/auth/drive
$this->_client->setAccessType($this->_config['accessType']); // offline
$token = $this->_client->refreshToken($this->_config['refreshToken']);
$this->_service = new \Google_Service_Drive($this->_client);
}
/**
* UPLOAD FILE
*/
public function uploadFile($filename, $file)
{
$gdriveFile = new \Google_Service_Drive_DriveFile();
$gdriveFile->title = $filename;
$chunkSizeBytes = 1 * 1024 * 1024;
// Call the API with the media upload, defer so it doesn't immediately return.
$this->_client->setDefer(true);
$request = $this->_service->files->insert($gdriveFile);
$mimeType = 'text/plain';
// Create a media file upload to represent our upload process.
$media = new \Google_Http_MediaFileUpload(
$this->_client,
$request,
$mimeType,
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize($file));
// Upload the various chunks. $status will be false until the process is
// complete.
$status = false;
$handle = fopen($file, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
// The final value of $status will be the data from the API for the object
// that has been uploaded.
$result = false;
if ($status !== false) {
$result = $status;
}
return $result;
}
/**
* DOWNLOAD FILE
*/
public function downloadUrl($fileId)
{
$file = $this->getFile($fileId);
if ($url = $file->getWebContentLink()) {
return $url;
}
return false;
}
/**
* GET FILE
* $file = $service->files->get($fileId);
*/
public function getFile($fileId)
{
try {
return $this->_service->files->get($fileId);
} catch(\Exception $e) {
throw new \Exception('Google Drive File Does Not Exist');
}
}
/**
* DELETE FILE
* $service->files->delete($fileId);
*/
public function deleteFile($fileId)
{
new \Debug();
}
/**
* FILE TO TRASH
* $service->files->trash($fileId);
*/
public function trashFile($fileId)
{
new \Debug();
}
/**
* EMPTY TRASH
* $service->files->emptyTrash();
*/
public function emptyTrash()
{
new \Debug();
}
/**
* FILE UNTRASH
* $service->files->untrash($fileId);
*/
public function untrashFile($fileId)
{
new \Debug();
}
/**
* LIST OF FILES
* $list = $service->files->listFiles();
*/
public function listOfFiles()
{
new \Debug();
}
}
但是,如果使用webContentLink我收到一个错误:403很抱歉,但你没有访问此页。我希望我的应用程序能够在没有登录的情况下向别人下载提供的文件。 – 2014-11-10 14:52:55
在这种情况下,请执行您已经在做的操作,但设置Content-Disposition标题,以便浏览器显示“另存为”对话框:http: //php.net/manual/en/function.http-send-content-disposition.php – 2014-11-10 16:00:20
感谢您的帮助,但我知道如果用户有链接,我可以将Google云端硬盘文件权限设置为公开。所以在这种情况下,webContentLnk的作品和解决问题。再次感谢和抱歉打扰你;-) – 2014-11-10 17:10:20