访问存储在Google云端硬盘中的文件的文本内容 - php api

时间:2016-04-05 21:14:16

标签: php api google-drive-api

我正在尝试获取存储在Google云端硬盘上的文件的文字内容(word和PDF),但经过大量研究后,我无法找到如何使其发挥作用。我正在使用Drive API,所有身份验证过程都正常。我可以使用API​​上传和访问文件。

认证后,我使用了GET代码:

$driveService = new Google_Service_Drive($client);   
$id = ‘XXXX’; 
$content = $driveService->files->get($id);
print_r($content);

我们测试中使用的文件是从API上传的“application / vnd.google-apps.document”。 它返回一个带有一些文件信息的对象但字段“webViewLink”e“webContentLink”是空白的,我真的不知道为什么? 对象返回:

Google_Service_Drive_DriveFile Object
(
    [collection_key:protected] => spaces
    [internal_gapi_mappings:protected] => Array
        (
        )

    [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] => XXXX
    [imageMediaMetadataType:protected] => Google_Service_Drive_DriveFileImageMediaMetadata
    [imageMediaMetadataDataType:protected] => 
    [kind] => drive#file
    [lastModifyingUserType:protected] => Google_Service_Drive_User
    [lastModifyingUserDataType:protected] => 
    [md5Checksum] => 
    [mimeType] => application/vnd.google-apps.document
    [modifiedByMeTime] => 
    [modifiedTime] => 
    [name] => 05042016121001_3548
    [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] => 
    [modelData:protected] => Array
        (
        )

    [processed:protected] => Array
        (
        )

)

我也尝试过以不同的方式,使用EXPORT方法。这是我使用的代码:

$content = $driveService->files->export($id, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', array(
  'alt' => 'media' ));

print_r($content);

它返回一个不同的对象,但我不知道是否可以使用它来获取文件内容。这是回报:

GuzzleHttp\Psr7\Response Object
(
    [reasonPhrase:GuzzleHttp\Psr7\Response:private] => OK
    [statusCode:GuzzleHttp\Psr7\Response:private] => 200
    [headers:GuzzleHttp\Psr7\Response:private] => Array
        (
            [expires] => Array
                (
                    [0] => Tue, 05 Apr 2016 20:10:33 GMT
                )

            [date] => Array
                (
                    [0] => Tue, 05 Apr 2016 20:10:33 GMT
                )

            [cache-control] => Array
                (
                    [0] => private, max-age=0, must-revalidate, no-transform
                )

            [content-disposition] => Array
                (
                    [0] => attachment
                )

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

            [content-type] => Array
                (
                    [0] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
                )

            [x-content-type-options] => Array
                (
                    [0] => nosniff
                )

            [x-frame-options] => Array
                (
                    [0] => SAMEORIGIN
                )

            [x-xss-protection] => Array
                (
                    [0] => 1; mode=block
                )

            [content-length] => Array
                (
                    [0] => 9985
                )

            [server] => Array
                (
                    [0] => GSE
                )

            [alternate-protocol] => Array
                (
                    [0] => 443:quic
                )

            [alt-svc] => Array
                (
                    [0] => quic=":443"; ma=2592000; v="32,31,30,29,28,27,26,25"
                )

        )

    [headerLines:GuzzleHttp\Psr7\Response:private] => Array
        (
            [Expires] => Array
                (
                    [0] => Tue, 05 Apr 2016 20:10:33 GMT
                )

            [Date] => Array
                (
                    [0] => Tue, 05 Apr 2016 20:10:33 GMT
                )

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

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

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

            [Content-Type] => Array
                (
                    [0] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
                )

            [X-Content-Type-Options] => Array
                (
                    [0] => nosniff
                )

            [X-Frame-Options] => Array
                (
                    [0] => SAMEORIGIN
                )

            [X-XSS-Protection] => Array
                (
                    [0] => 1; mode=block
                )

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

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

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

            [Alt-Svc] => Array
                (
                    [0] => quic=":443"; ma=2592000; v="32,31,30,29,28,27,26,25"
                )

        )

    [protocol:GuzzleHttp\Psr7\Response:private] => 1.1
    [stream:GuzzleHttp\Psr7\Response:private] => GuzzleHttp\Psr7\Stream Object
        (
            [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #220
            [size:GuzzleHttp\Psr7\Stream:private] => 
            [seekable:GuzzleHttp\Psr7\Stream:private] => 1
            [readable:GuzzleHttp\Psr7\Stream:private] => 1
            [writable:GuzzleHttp\Psr7\Stream:private] => 1
            [uri:GuzzleHttp\Psr7\Stream:private] => php://temp
            [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
                (
                )

        )

)

我在网上进行了大量研究,大多数都是在stackoverflow上进行的。我找到了一些信息,但不是我想要做的解决方案。感谢有人可以帮助我。

0 个答案:

没有答案