0

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!

SILminore
  • 509
  • 3
  • 10
  • 28

1 Answers1

5

You are expecting $doc to be an object, which it is not because the Google client libraries are configured to return data arrays instead of objects by default.

To change this behavior without modifying the original source you can add a local_config.php file next to the existing config.php that has these contents:

<?php

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

The client libraries will detect and use this configuration automatically.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • Huh, @Jon, where I should upload this local_config.php? And why it is NOT necessary with INSERT script? Thanks! – SILminore Apr 10 '13 at 10:06
  • @Huxley: Next to the existing `config.php` of the Google API client. The insert script as given does not need this because it never uses the return value of any API function as an object. If you tried to do `$output->anything(...)` on the insert sample you would have the same problem there as well. – Jon Apr 10 '13 at 10:09
  • Thats absolutely elegant, perfect solution! – SILminore Apr 10 '13 at 10:11
  • Should we or shouldn't we add "?>" wt the end of this php file.. did u miss it. or it has any reason? – Vishwanath gowda k Mar 07 '14 at 16:52
  • @Vishwanathgowdak: There's no need to add it, and it helps you avoid problems due to extra whitespace after the closing tag (see accepted answer [here](http://stackoverflow.com/q/8028957/50079)). – Jon Mar 07 '14 at 20:54