Reputation: 31310
The Google documentation example for setting up the Google Drive API to work with PHP, has require statements like this:
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
other code
?>
I downloaded a .zip file from GitHub, and extracted it. In the extracted folders and files, the google-api-php-client files from GitHub, do not contain a file named Google_Client.php. There is a file named, Client.php in the src/Google/ directory.
And there is no contrib folder either.
AND, there is no file named DriveService.php in the download of the GitHub google-api-php-client files.
So, I'm assuming that the references to the files and folders should look like this:
<?php
require_once 'google-api-php-client/src/Google/Client.php';
require_once 'google-api-php-client/src/Google/Drive.php';
other code
?>
The Client.php file contains a require_once
statement, for an autoload.php file:
if (!class_exists('Google_Client')) {
require_once dirname(__FILE__) . '/../autoload.php';
}
The double dots in the reference /../
indicates that when the file autoload.php is being searched for in order to include, go up to the parent folder, and search there.
So the folder structure should be:
src - Folder
autoload.php
Google - Folder
Client.php
DriveService.php
The autoload.php file code is:
<?php
function google_api_php_client_autoload($className)
{
$classPath = explode('_', $className);
if ($classPath[0] != 'Google') {
return;
}
// Drop 'Google', and maximum class file path depth in this project is 3.
$classPath = array_slice($classPath, 1, 2);
$filePath = dirname(__FILE__) . '/' . implode('/', $classPath) . '.php';
if (file_exists($filePath)) {
require_once($filePath);
}
}
spl_autoload_register('google_api_php_client_autoload');
The Drive.php file has no require
or include
statements in it.
There is also a Service folder in the downloaded files, and there is a Service.php file in the Google folder.
Is the documentation wrong, and my suggestion right? What is the correct set up for the Drive API in PHP?
Upvotes: 0
Views: 757
Reputation: 31310
The folder structure from the zip file from GitHub is different than the folder structure of files and folders obtained from Subversion. When the Google API PHP Client Library is downloaded with Subversion, there is a contrib folder, and there are files named Google_Client.php and Google_DriveService.php. I had downloaded and extracted files from GitHub from a zip file. That creates a very different outcome then downloading the files with Subversion. Subversion renames the files I guess, and creates different folder names. So, with the new download of folders and files using Subversion, the folder structure and file names confer with the example in the documentation.
From a Getting Started page in some old documentation:
https://code.google.com/p/google-api-php-client/wiki/GettingStarted
I found some info:
After extracting the library . . . . . , you will have a new google-api-php-client-x.y directory. The library files themselves are in the google-api-php-client/src directory
So, you need to end up with a:
google-api-php-client
Directory, and a:
google-api-php-client/src
directory.
Quote:
After obtaining the library . . , you will have a directory named google-api-php-client somewhere on your file system, containing the library files. Specifically, you will need to include the file src/Google_Client.php and src/contrib/Google_PlusService.php inside of your scripts.
The above quote is for Google_PlusService.php but that could be replace with whatever API service file you need to use.
There are multiple ways to include the needed library files into your code.
It seems that there was a change in naming conventions of files and folders that is explained in the documentation for migrating to a new library version.
https://developers.google.com/api-client-library/php/guide/migration
Before the Drive API can be used, authorization needs to take place. The initial documentation I was looking at was for a command line application. I don't want a command line application, I'm trying to build a web application. So, server side authorization needs to be implemented first.
https://developers.google.com/drive/web/auth/web-server
The server side authorization needs an additional php file included into your code.
require_once "google-api-php-client/src/Google/Service/Oauth2.php";
The documentation is here:
https://developers.google.com/drive/web/auth/web-server
Upvotes: 1