2016-08-23 7 views
0

上記のhereのAPIを使用してGoogleドライブからファイルをダウンロードしています。フローでは、ユーザーの認証に合格することができ、以下のコードでファイルを一覧表示することができました。Google API v3 from PHP

echo "<br>" . count($results->getFiles()). " File(s) found" ; 
     echo "<hr>"; 
     foreach ($results->getFiles() as $file) { 
      echo "File Name is " . $file->getName() . " And file ID is ". $file->getId() . " Type is ".$file->getMimeType() ."<br><pre>"; 
      print_r($file); 
      echo "</pre><hr>"; 
     } 

、出力が

$fileId = '0B5CWXJTNwALMOEJkdzFvMVVsVFE'; 
     $content = $drive_service->files->get($fileId, array(
      'alt' => 'media')); 
     echo "<pre>"; 
     print_r($content->getHeaders()); 
     foreach ($content->getHeaders() as $name => $values) { 

     header($name . ': ' . implode(', ', $values[0])); 
     } 

ファイルがダウンロードされていない以下のようにダウンロードコードを適用しようと、今

100 File(s) found 
File Name is invoice receipt.pdf And file ID is 0B5CWXJTNwALMOEJkdzFvMVVsVFE Type is application/pdf 
Google_Service_Drive_DriveFile Object 
(
    [collection_key:protected] => spaces 
    [appProperties] => 
    [capabilitiesType:protected] => Google_Service_Drive_DriveFileCapabilities 
    [capabilitiesDataType:protected] => 
    [contentHintsType:protected] => Google_Service_Drive_DriveFileContentHints 
    [contentHintsDataType:protected] => 
    [createdTime] => 
    [description] => 
    [explicitlyTrashed] => 
    [fileExtension] => 
    [folderColorRgb] => 
    [fullFileExtension] => 
    [headRevisionId] => 
    [iconLink] => 
    [id] => 0B5CWXJTNwALMOEJkdzFvMVVsVFE 
    [imageMediaMetadataType:protected] => Google_Service_Drive_DriveFileImageMediaMetadata 
    [imageMediaMetadataDataType:protected] => 
    [isAppAuthorized] => 
    [kind] => drive#file 
    [lastModifyingUserType:protected] => Google_Service_Drive_User 
    [lastModifyingUserDataType:protected] => 
    [md5Checksum] => 
    [mimeType] => application/pdf 
    [modifiedByMeTime] => 
    [modifiedTime] => 
    [name] => invoice receipt.pdf 
    [originalFilename] => 
    [ownedByMe] => 
    [ownersType:protected] => Google_Service_Drive_User 
    [ownersDataType:protected] => array 
    [parents] => 
    [permissionsType:protected] => Google_Service_Drive_Permission 
    [permissionsDataType:protected] => array 
    [properties] => 
    [quotaBytesUsed] => 
    [shared] => 
    [sharedWithMeTime] => 
    [sharingUserType:protected] => Google_Service_Drive_User 
    [sharingUserDataType:protected] => 
    [size] => 
    [spaces] => 
    [starred] => 
    [thumbnailLink] => 
    [trashed] => 
    [version] => 
    [videoMediaMetadataType:protected] => Google_Service_Drive_DriveFileVideoMediaMetadata 
    [videoMediaMetadataDataType:protected] => 
    [viewedByMe] => 
    [viewedByMeTime] => 
    [viewersCanCopyContent] => 
    [webContentLink] => 
    [webViewLink] => 
    [writersCanShare] => 
    [internal_gapi_mappings:protected] => Array 
     (
     ) 

    [modelData:protected] => Array 
     (
     ) 

    [processed:protected] => Array 
     (
     ) 

) 

のようなものですが、は次のように出力を見ることができました以下

Array 
(
    [X-GUploader-UploadID] => Array 
     (
      [0] => AEnB2UrDOQ-mYxNd5tE5ctVr01jVTnh3ZykKviCBnEOjDRGtT_EIT2nbHLEM_f-2pZ22anDJpqMzbISzhcTTZY3PgsDj69PE-w 
     ) 

    [Content-Type] => Array 
     (
      [0] => application/pdf 
     ) 

    [Content-Disposition] => Array 
     (
      [0] => attachment 
     ) 

    [Vary] => Array 
     (
      [0] => Origin 
      [1] => X-Origin 
     ) 

    [Expires] => Array 
     (
      [0] => Tue, 23 Aug 2016 13:20:18 GMT 
     ) 

    [Date] => Array 
     (
      [0] => Tue, 23 Aug 2016 13:20:18 GMT 
     ) 

    [Cache-Control] => Array 
     (
      [0] => private, max-age=0, must-revalidate 
     ) 

    [Content-Length] => Array 
     (
      [0] => 80277 
     ) 

    [X-Goog-Hash] => Array 
     (
      [0] => crc32c=5yKN2g== 
     ) 

    [Server] => Array 
     (
      [0] => UploadServer 
     ) 

    [Alternate-Protocol] => Array 
     (
      [0] => 443:quic 
     ) 

    [Alt-Svc] => Array 
     (
      [0] => quic=":443"; ma=2592000; v="35,34,33,32,31,30" 
     ) 

) 

質問:ファイルをダウンロードするにはどうすればよいですか?パウロの回答に基づいて

EDIT

below.Thisは、画像/ PNGのために働いて、アプリケーション/オクテットストリーム、アプリケーション/ PDFの種類、しかしZIP/RARファイルの問題を持っているされて、2つのファイルを作成しました(エラー:このサイトはに到達できない

として

test.phpを

<?php 
require_once __DIR__ . '/vendor/autoload.php'; 
session_start(); 

$client = new Google_Client(); 
$client->setAuthConfigFile('client_secrets.json'); 
$client->addScope(Google_Service_Drive::DRIVE); 

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) 
{ 
    $client->setAccessToken($_SESSION['access_token']); 
    $drive_service = new Google_Service_Drive($client); 
    $results = $drive_service->files->listFiles(); 
    if (count($results->getFiles()) == 0) 
    { 
     echo "No files found"; 
    } 
    else 
    { 
     echo "<br>" . count($results->getFiles()). " File(s) found" ; 
     echo "<br><table><thead><th>File Name</th><th>File Type</th><th>Download Link</th></thead><tbody>"; 
     foreach ($results->getFiles() as $file) { 
      echo "<tr><td>" . $file->getName() . "</td><td>" . $file->getMimeType() ."</td><td><a href='gdrivedownlaod.php?fileid=". $file->getId() . "&type=" . $file->getMimeType() . "&name=" . $file->getName() . "' target='blank'>Download</a></td></tr>";// ."<br><pre>"; 
      } 
      echo "</tbody></table>"; 
    } 

} 
else 
{ 
    $redirect_uri = 'https://abcd.co/oauth2callback'; 
    echo "Hello, <a href='" . $redirect_uri . "'>Log in with Drive</a>"; 
} 

とgdrivedownlaod.php

+0

ありがとうございました。変更しました。ソリューションに向けた考え方は? – Rajesh

+0

[docs](https://developers.google.com/drive/v3/web/manage-downloads#using_altmedia)のファイルをダウンロードするための提案を、ファイルのリソースURLに許可されたHTTP GET要求を行い、クエリパラメータalt = media。この方法が有効な場合は、使用しているコードをもう一度チェックしてみてください。ファイルをダウンロードするには、ユーザーに少なくとも読み取りアクセス権が必要であることに注意してください。あなたのアプリは、ファイルの内容を読むことができる範囲で許可されていなければなりません。たとえば、drive.readonly.metadataスコープを使用するアプリケーションは、ファイルの内容をダウンロードする権限がありません – KENdi

+0

Thanks @KENdi。私はメタデータを得ることができるようにフルドライブアクセスを持っています。問題は、ブラウザーでファイルをディスパッチするためにグーズレスポンスを使用する方法です。 – Rajesh

答えて

1
$fileName = 'xpto.pdf'; 
$driveService = new Google_Service_Drive($client); 
$content = $driveService->files->get($fileId, array(
    'alt' => 'media')); 

$headers = $content->getHeaders(); 
foreach ($headers as $name => $values) { 
    header($name . ': ' . implode(', ', $values)); 
} 
header('Content-Disposition: attachment; filename="' . $fileName . '"'); 
echo $content->getBody(); 
exit; 
+0

@ paulo-pintoありがとうございますが、ファイルをエクスポートする代わりにrawコンテンツを出力します。 – Rajesh

+0

@Rajeshヘッダー( 'Content-Disposition:attachment; filename = "'。$ fileName。 '"'); ( 'Content-Disposition:inline; filename = "'。$ fileName。 '"');ブラウザで見るために。あなたは新しいタブでリンクを開いていますか? –

+0

が質問を編集しました。gdrivedownlaod.phpを確認して変更を提案してください – Rajesh

0

上記のとおり$content->getHeaders()は、HTTPリクエストの対応するヘッダー情報の出力を提供しています。

ファイルコンテンツを取得するには、$content->getResponseBody()が必要です。次に、データをローカルファイルに保存するためにfile_put_contentsを呼び出します。

+0

あなたはそれの完全なスクリプトの部分で助けてください。私は '' 'print_r($ content-> getResponseBody());しようとしました;' '何も印刷されません。 – Rajesh

+0

私はPHPにうまくいかず、とにかくドキュメントをチェックすることでそれを理解できるはずです(怠け者ではありません。試してみると特定のエラーに遭遇し、ここに詳細を投稿すれば、手助け)。ちなみに、あなたのファイルはPDFであり、ファイルの内容は通常バイナリデータですが、あなたはそれについてprint_rを行うことはできません。がんばろう。 – some1

+0

私はすでに質問をする前に十分試してみました。画像タイプも試してみましたが結果は同じです。 pdfの場合でも、印刷されていなければ、それは起こらなかったファイルをダウンロードすることを期待していました。ここの問題は、SDKがまだベータ版であり、V2 APIが廃止されていることです。 – Rajesh