首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >文件更新不再使用google-api-php-client

文件更新不再使用google-api-php-client
EN

Stack Overflow用户
提问于 2014-06-03 06:46:34
回答 1查看 581关注 0票数 1

我有一个非常小的上传和/或更新文件的脚本(大约1年前写的,借用了示例中80%的代码)

自3月以来代码没有重大变化(使用1.0.0-alpha),但在5月中旬文件更新停止工作,引发内部服务器错误,我升级到1.0.4-beta但没有成功:

代码语言:javascript
复制
PHP Fatal error:  Uncaught exception 'Google_Service_Exception' with message 'Error calling PUT https://www.googleapis.com/upload/drive/v2/ [...] (500) Internal Error' in 
google-api-php-client-1.0.4-beta/src/Google/Http/REST.php:80

代码:

代码语言:javascript
复制
$client->setDefer(true);
$request = $service->files->update($update_id,$file);

$media = new Google_Http_MediaFileUpload(
    $client,
    $request,
    'text/plain',
    null,
    true,
    $chunkSizeBytes
);
$media->setFileSize(filesize($csvfile))
$status = false;
$handle = fopen($csvfile, "rb");
while (!$status && !feof($handle)) {
    $chunk = fread($handle, $chunkSizeBytes);
    $status = $media->nextChunk($chunk);
}

文件插入(HTTP POST)仍然有效(使用与上传块相同的代码)

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2014-09-07 02:32:57

我可以使用以下代码更新文件(以前创建的或从模板复制的):

代码语言:javascript
复制
     (...)
     require_once 'src/Google/Client.php';
     require_once 'src/Google/Service/Oauth2.php';
     require_once 'src/Google/Service/Drive.php';    
     (...)

     $client = new Google_Client();
     // set scopes ...
     (...)

     $GDrive_service = new Google_Service_Drive($client);
     (...)


     // then I set parameters 
        (...)
        $newTitle = $title ;
        $newDescription = $description ;
        $newMimeType = 'text/csv' ;
        $newFileName = $filename ;
        $service = $GDrive_service ;
        (...)



$updatedFile = updateFile($service, $fileId, $newTitle, $newDescription, $newMimeType, $newFileName, $newRevision) ;


function updateFile($service, $fileId, $newTitle, $newDescription, $newMimeType, $newFileName, $newRevision) {
  try {
    // First retrieve the file from the API.
    $file = $service->files->get($fileId);

    // File's new metadata.
    $file->setTitle($newTitle);
    $file->setDescription($newDescription);
    $file->setMimeType($newMimeType);

    // File's new content.
    $data = file_get_contents($newFileName);
    $convert = 'true' ;

    $additionalParams = array(
      'uploadType' => 'multipart',
      'newRevision' => $newRevision,
      'data' => $data,
      'mimeType' => $newMimeType,
      'convert' => $convert,
    );

    // Send the request to the API.
    $updatedFile = $service->files->update($fileId, $file, $additionalParams);
    return $updatedFile;
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
}


(...)
// this is info of the updated file 
$fileId = $updatedFile->getId() ;
// link of file
$cF_link = $updatedFile->alternateLink ;
// pdf version
$enllacos = $updatedFile->exportLinks ;
$cF_PDF_link = $enllacos['application/pdf'] ;
(...)

此代码适用于1.0.5-beta版本的php客户端

Sergi

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24004489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档