"PHP致命错误:在Google Drive php UPDATE脚本中调用非对象上的成员函数setTitle()

Hux*_*ley 0 php google-api google-drive-api google-api-php-client

我正在使用这个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);
Run Code Online (Sandbox Code Playgroud)

现在我想更新(不上传)我现有的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);
Run Code Online (Sandbox Code Playgroud)

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

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

我已经按照这个参考.请问您可以帮我解决这个问题,或者您可以建议通过php将文件更新到Google云端硬盘的准确和正确的代码吗?谢谢!

Jon*_*Jon 5

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

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

<?php

$apiConfig = array(
    'use_objects' => true,
);
Run Code Online (Sandbox Code Playgroud)

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