0

我正在使用这个 php 脚本将文件插入(上传)到我的 Google 云端硬盘,并且非常完美:

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

$drive = new Google_Client();

$drive->setClientId('XXX');
$drive->setClientSecret('YYY');
$drive->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$drive->setScopes(array('https://www.googleapis.com/auth/drive'));

$gdrive = new Google_DriveService($drive);

$drive->setAccessToken(file_get_contents('token.json'));

$doc = new Google_DriveFile();

$doc->setTitle('Test');
$doc->setDescription('Test Document');
$doc->setMimeType('text/plain');

$content = file_get_contents('test.txt');

$output = $gdrive->files->insert($doc, array(
      'data' => $content,
      'mimeType' => 'text/plain',
    ));

print_r($output);

现在我想更新(不上传)我现有的 Google Drive 文件,我正在使用这个脚本:

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

$drive = new Google_Client();

$drive->setClientId('XXX');
$drive->setClientSecret('YYY');
$drive->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$drive->setScopes(array('https://www.googleapis.com/auth/drive'));

$gdrive = new Google_DriveService($drive);

$drive->setAccessToken(file_get_contents('token.json'));

$fileId = "ZZZ";
$doc = $gdrive->files->get($fileId);

$doc->setTitle('Test'); // HERE I GET THE ERROR "CALL TO A MEMBER FUNCTION SETTITLE()..."
$doc->setDescription('Test Document');
$doc->setMimeType('text/plain');

$content = file_get_contents('test.txt');

$output = $gdrive->files->update($fileId, $doc, array(
      'newRevision' => $newRevision,
      'data' => $content,
      'mimeType' => 'text/plain',
    ));

print_r($output);

不幸的是,我收到了这个错误:

PHP Fatal error: Call to a member function setTitle() on a non-object in line $doc->setTitle...

我遵循了这个参考。请你能帮我解决这个问题,或者你能建议通过php将文件更新到谷歌驱动器的精确和正确的代码吗?谢谢!

4

1 回答 1

5

您期望$doc成为一个对象,这不是因为 Google 客户端库被配置为默认返回数据数组而不是对象

要在不修改原始源的情况下更改此行为,您可以在具有以下内容local_config.php的现有文件旁边添加一个文件:config.php

<?php

$apiConfig = array(
    'use_objects' => true,
);

客户端库将自动检测并使用此配置。

于 2013-04-10T10:02:16.217 回答