Preconditions
Composer is installed.
Installation
To install the library in the local development environment:
In terminal, navigate (
cd) to the project root directory.Run the following command:
composer require google/apiclient:^2.15.0
Change 2.15.0 to the latest version of the library.
This will install all Google APIs services that are included in this library. By navigating to
/project-root/vendor/google/apiclient-services/src, we can see all of the services that were installed. To keep only the services that we’re actually going to use, edit thecomposer.jsonfile in the project root directory.Add the
pre-autoload-dumpscript and the list of required services (google/apiclient-services) so it looks similar to this:
{
"require": {
"google/apiclient": "^2.15.0"
},
"scripts": {
"pre-autoload-dump": "Google\\Task\\Composer::cleanup"
},
"extra": {
"google/apiclient-services": [
"Drive",
"Sheets"
]
}
}
In the example above, we keep only the Google Drive and Google Sheets services.
- Run the following command to apply the changes:
composer update
- Include the autoloader where the API service is used:
require_once '/project-root/vendor/autoload.php';
On a WordPress site:
require_once ABSPATH . 'vendor/autoload.php';
Add a new Google API service
If we need to expand the list of API services previously defined in google/apiclient-services:
Add the new service name to the
google/apiclient-serviceslist in thecomposer.jsonfile.In terminal, navigate (
cd) to the project root directory.Run:
rm -r vendor/google/apiclient-services
- Run:
composer update
If we don’t know the name of the new service
- We can temporarily remove the
pre-autoload-dumpline and thegoogle/apiclient-serviceslist fromcomposer.json. - In terminal, navigate (
cd) to the project root directory. - Run:
rm -r vendor/google/apiclient-services
- Run:
composer update
- All Google API services will be installed.
- Navigate to
/project-root/vendor/google/apiclient-services/src. - Find there the new service that is needed and copy its name.
- Restore the
composer.jsonfile (re-add thepre-autoload-dumpline and thegoogle/apiclient-serviceslist). - Add the new service name to the
google/apiclient-serviceslist. - Run:
composer update
Installation in production
You can learn more about how I install the PHP dependencies on the production server in my other post.