Php “Google驱动器API httpRequest下载文件”对话框

Php “Google驱动器API httpRequest下载文件”对话框,php,google-api,google-drive-api,Php,Google Api,Google Drive Api,我是PHP新手,一直在使用GoogleDrivePHPAPI 这是我的代码: function downloadFile1($service,$downloadUrl,$client) { if ($downloadUrl) { $request = new Google_Http_Request($downloadUrl, 'GET', null, null); $SignhttpRequest = $client->getAuth()->sig

我是PHP新手,一直在使用GoogleDrivePHPAPI

这是我的代码:

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将文件下载到服务器上,而是在页面上向用户显示指向文件名称的锚点。单击链接将文件下载到用户的计算机。

您好,这段代码可能会帮助用户使用google drive作为一项服务,可与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很抱歉,您无权访问此页面。我希望我的应用程序能够在不登录的情况下将provate文件下载给某人。在这种情况下,请执行您正在执行的操作,但要设置内容处置标题,以便您的浏览器显示“另存为”对话框:感谢您的帮助,但我意识到,如果用户有链接,我可以将Google Drive文件权限设置为Public。因此,在这种情况下,webContentLnk起作用,问题就解决了。再次感谢,很抱歉打扰您;-)