Alan Wells
Alan Wells

Reputation: 31310

google-api-php-client - Get File Contents

Using the following code:

// get the file contents

$file = new Google_Service_Drive_DriveFile();
$fileId = 'The File ID';

try {
  echo 'try part <br>';
  $result3 = $service->files->get($fileId);

  echo 'Contents: ' . $result3->getContent().'<br>';
  echo "Title: " . $result3->getTitle() . '<br>';
  echo "Description: " . $result3->getDescription() . '<br>';
  echo "MIME type: " . $result3->getMimeType() . '<br>';
} catch (Exception $e) {
  echo "An error occurred: " . $e->getMessage();
};

Everything works except for this line:

echo 'Contents: ' . $result3->getContent().'<br>';

In the drive.php file in the API, there is a getContent() function, but it's not working. How do I get the content out of the file?

Upvotes: 0

Views: 2777

Answers (1)

pinoyyid
pinoyyid

Reputation: 22306

If you refer to the Google documentation at https://developers.google.com/drive/v2/reference/files/get you'll see that there are two forms of the Get method. The default fetches the metadata (eg. title, description, etc), whereas alt=media fetches the content. Alternatively, you can also fetch the content via the downloadUrl property. I don't know if the PHP library exposes the alt=media option (I don't like libraries), but the downloadUrl approach has an example on the page I linked, just scroll down and click "php"

Upvotes: 1

Related Questions