我是PHP新手,一直在使用Google Drive PHP API。
这是我的代码:
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);]
我可以在网页上看到文件的内容,但我想下载它!]
提前感谢!
不要使用PHP将文件下载到服务器,而是在页面上向用户显示一个指向文件的webContentLink
的锚点。单击链接将把文件下载到用户的计算机上。
Hi All也许这段代码将帮助人们使用谷歌驱动器作为一项服务,该服务可以与cronjob/cli一起使用,以上传、删除和下载文件大小不受限制的文件。
它还没有完成,但它是谷歌apiclient的包装。并且可以用于上传、下载和删除文件。
<?php
namespace AppModelGoogle;
class Drive
{
public $pixie;
protected $_config;
protected $_client;
protected $_service;
public function __construct(AppPixie $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();
}
}