我正在使用这个 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将文件更新到谷歌驱动器的精确和正确的代码吗?谢谢!