我正在使用这个php脚本 INSERT (上传)一个文件到我的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云端硬盘文件,我正在使用此脚本:
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...
我已关注THIS reference.请您帮助我解决此问题,或者您是否可以建议通过php将文件更新到Google云端硬盘的准确和正确的代码?谢谢!
答案 0 :(得分:5)
您希望$doc
成为对象,而不是因为Google客户端库是configured to return data arrays instead of objects by default。
要在不修改原始来源的情况下更改此行为,您可以在包含这些内容的现有local_config.php
旁边添加config.php
文件:
<?php
$apiConfig = array(
'use_objects' => true,
);
客户端库将自动检测并使用此配置。