&引用;PHP致命错误:对非对象调用成员函数setTitle();在Google驱动php更新脚本中

&引用;PHP致命错误:对非对象调用成员函数setTitle();在Google驱动php更新脚本中,php,google-api,google-drive-api,google-api-php-client,Php,Google Api,Google Drive Api,Google Api Php Client,我正在使用此php脚本将一个文件插入(上传)到我的谷歌硬盘,其完美之处在于: 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->set

我正在使用此php脚本将一个文件插入(上传)到我的谷歌硬盘,其完美之处在于:

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文件?谢谢

您希望
$doc
是一个对象,这不是因为Google客户端库是

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

<?php

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

您希望
$doc
是一个对象,这不是因为Google客户端库是

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

<?php

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

显然,
doc=$gdrive->files->get($fileId)
不返回有效文件,因此请确保您使用的是正确的文件ID。@JevgenijEvll已被控制,但不幸的是ID为true:(很明显,
$doc=$gdrive->files->get($fileId);
不返回有效文件,因此请确保您使用的是正确的文件ID。@JevgenijEvll已被控制,不幸的是true:(哈,@Jon,我应该把这个本地的_config.php上传到哪里?为什么不需要插入脚本?谢谢!@Huxley:在Google API客户端现有的
config.php
旁边。给定的插入脚本不需要这个,因为它从不使用任何API函数的返回值作为对象。如果你尝试执行
$output->anything(…)
在插入示例中,您也会遇到同样的问题。这是绝对优雅的完美解决方案!我们是否应该添加“?>”wt此php文件的结尾..您是否错过了它。或者它有什么原因?@Vishwanathgowdak:不需要添加它,它可以帮助您避免由于结束标记后的额外空格而导致的问题(请参阅已接受的答案).Huh,@Jon,我应该把这个本地的_config.php上传到哪里?为什么不需要插入脚本?谢谢!@Huxley:在Google API客户端现有的
config.php
旁边。给定的插入脚本不需要这个,因为它从不使用任何API函数的返回值作为对象。如果您尝试执行
$output->anything(…)
在插入示例中,您也会遇到同样的问题。这是绝对优雅的完美解决方案!我们是否应该添加“?>”wt此php文件的结尾..您是否错过了它。或者它有什么原因?@Vishwanathgowdak:不需要添加它,它可以帮助您避免由于结束标记后的额外空格而导致的问题(请参阅已接受的答案)。