【问题标题】:Google Drive API v3 PHP: Upload a file Call to undefined methodGoogle Drive API v3 PHP:上传文件调用未定义的方法
【发布时间】:2016-02-18 15:37:15
【问题描述】:

我已按照https://developers.google.com/drive/v3/web/quickstart/php 的快速入门进行操作,一切正常。

我可以搜索文件,下载文件,但我不能上传文件。

我在这里使用这种方法: https://developers.google.com/api-client-library/php/guide/media_upload

$file = new Google_Service_Drive_DriveFile();
$result = $service->files->insert($file, array(
   'data' => file_get_contents("path/to/file"),
   'mimeType' => 'application/octet-stream',
   'uploadType' => 'media'
));

但我总是收到这样的错误:

Fatal error: Uncaught Error:
Call to undefined method Google_Service_Drive_Files_Resource::insert()

我错过了什么?

【问题讨论】:

    标签: php google-drive-api google-api-php-client


    【解决方案1】:

    insert 方法已更改为 create

    https://developers.google.com/drive/v3/reference/files/create

    另一个陷阱:$file->setTitle 也不起作用(使用 setName)。

    最终代码如下所示:

    $file = new Google_Service_Drive_DriveFile();
    $file->setName($filename);
    $result = $service->files->create($file, array(
      'data' => file_get_contents("path/to/file"),
      'mimeType' => 'application/octet-stream',
      'uploadType' => 'media'
    ));
    

    【讨论】:

    • 什么是`$service`?这个怎么导入?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    相关资源
    最近更新 更多