Google Drive API v3 PHP:上传文件调用未定义的方法


Google Drive API v3 PHP: Upload a file Call to undefined method

我已经按照 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()

我错过了什么?

插入方法已更改为创建

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'
));