I'm using this php script to INSERT (upload) a file to my Google Drive, and its perfect:
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);
Now I want to UPDATE (not upload) my existing Google Drive file, and I'm using this script:
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);
Unluckly I get this error:
PHP Fatal error: Call to a member function setTitle() on a non-object in line $doc->setTitle...
I have followed THIS reference. Please can u help me to resolve the issue, or can you suggest the precise and right code to UPDATE a file to Google Drive through php? Thanks!