Google API Client Libraries PHP - Remove unnecessary folders and files - php

I just follow the instructions provided by google team, to communicate with the shopping API:
https://developers.google.com/api-client-library/php/auth/service-accounts
Everything works fine, I complete my integration (API for Shopping), nevertheless I realise now that when I download this google library, using composer:
composer require google/apiclient:^2.0
I have now around 6300 files to commit!!!
So, I will like to try to remove what for sure I will not need, since I just use API for google Shopping.
This is my current vendor/ ( or lib/) folders structures:
This a sample of my code that is already working correctly:
require_once __DIR__.'/lib/autoload.php';
$client = new Google_Client();
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
$client->useApplicationDefaultCredentials();
$client->setScopes('https://www.googleapis.com/auth/content');
$client->fetchAccessTokenWithAssertion();
$service = new Google_Service_ShoppingContent($client);
Even I never use some of features directly located in other folders (ex: monolog, etc), I'm not sure if they are or not necessary (I suppose not) for google api.
So I will like to have confirmation, of the folders that could be removed safely and everything that allow google api shopping works correctly are not affected.

All of those files are required dependencies of the google API project. You should never commit composer's vendor directory into your VCS repository. You should utilize the feature of your VCS for ignoring files/directories to prevent you from accidentally committing those dependency files.

Related

About Google Calendar API and PHP

I am trying to write a webpage that will present to the public the contents of one of my Google Calendars. The standard iframe doesn't give me the format I'd like and I also want to embed some schema.org event information, update and submit a sitemap, etc.
As I'm fairly experienced with PHP, I'm trying to extract data from the Google Calendar API. I looked at Google's quickstart https://developers.google.com/calendar/quickstart/php but it assumes a level of knowledge I don't possess.
It seems I need to use something called "composer" and tells me I need to do "composer require google/apiclient:^2.0" but I'm unclear where exactly I type this - I can't see anywhere obvious in cPanel - or maybe it goes at the start of my PHP code?
Also, I note that the sample code is command-line only and I can't find code anywhere that would appear to be capable of running as a PHP web page. Is that because it's just not possible?
Composer is a dependency manager like bower, npm, yarn etc., the list goes on and on. Firstly you'll have to install composer here and add it to your PATH variable (method depends on OS).
Afterwards, when you create a directory for your project in the www-folder of your WAMP/LAMP or another folder acting as webroot, you can execute the composer require command. This will generate a folder called 'vendor' (with your dependency as a subdirectory) and a composer.json & composer.lock file, the latter 2 files will contain all the 'required dependencies' of composer.
You can then include the dependency using the require_once()-function in your PHP-script.
Hopefully this helps.
Cheers.

Pusher PHP API: what are the minimum files to keep in Production?

Using Pusher in Dev with the PHP APi (pusher-php-server) works fine, but I installed it with composer and it brought a large amount of files including tests and many more stuff that seem irrelevant for my Prod deployment.
Is there an easy way to find out what files can be removed and how to use the PHP API without the fancy autoload etc, to slim out the number of files to be kept for Prod?
I currently have in mu Pusher API root:
composer
paragonie
psr
pusher
autoload.php
For a total of little less than 3MB.
I figured the core of it is placed here: ./pusher/pusher/pusher-php-server/src but I have no idea how to use it properly since currently my php code does:
require '../ext/pusher/autoload.php';
$pusher = new Pusher\Pusher([...]);
$pusher->trigger([...]);
(this code is called every time a message needs to be sent to the channel)

Google PHP API too many files

I am trying to create a plugin for WordPress using the Google Analytics Reporting API v4 https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php
We need to load the Google API PHP Client Library
require_once __DIR__ . '/vendor/autoload.php';
I have downloaded the latest release of Google API https://github.com/google/google-api-php-client/releases but the vendor folder contains more than 7000 files!
My question is if I am doing something wrong here or I am missing something? Inside the vendor folder I can see all the Google services. How I can use the Google API with less files in a WordPress plugin? I will appreciate any help to the right direction.
By default the Google/Service directory contains all the code needed to access every Google Discovery services API. You don't technically need all of the files. You can safely delete everything under Google/Service that you don't need.
Dont delete files from any of the other directories. Just remove the unneeded APIs.

Google API PHP client with Composer

I made a WP plugin which depends on the Youtube API. So, to do it well, I use Google API PHP client.
My issue is quite simple: the Google API PHP client is huge (more than 12 000 files), including clients for all Google services, when I just need to use Youtube service. So I'm not confortable committing all those files to the WP plugins repo when most of them are useless in my case.
So, right now, my composer.json looks like this:
{
"require": {
"google/apiclient": "^2.0"
}
}
Any way to use only the Youtube API client (using composer if it's possible)?
This is not for the feign of heart but it is doable.
Go to this branch of the repo https://github.com/google/google-api-php-client/tree/v1-master
Grab the full src/Google directory you will need all of that.
In the src/Google/Service directory is the stuff for all the different APIs. Remove everything except for YouTube.
cross your fingers it should work.
This wont remove all 12000 files but it should give you just what you absolutely need. I used to do this but its been a few years.
Unfortunately, I don't think this is possible without manually including the relevant files in your project (which is a really bad idea).
The reason why its impossible, is because Google places all service APIs for the PHP library in a single GitHub repository found here:
https://github.com/google/google-api-php-client-services
If Google does split them into multiple repositories, then it could be possible to include a single repo.
For now, though, all services are under one composer project and one repo. Maybe its worth making a GitHub issue to ask Google about this?

PHP Composer - How to work with multiple vendors?

The project I'm working on requires using the PHP SDK's from multiple 3rd parties. Two of these are Amazon Web Services and the Google API Client (for Google+), and both of them use Composer to manage their files / dependencies. I'm not sure how to best set it up code / structure wise, though, because I don't need both AWS and Google loaded together. I might need AWS in one area and Google in another, so I don't want to just autoload everything every time and have the additional overhead from libraries I don't need right then. Right now I have the structure set up like this:
awscode.php
googlecode.php
libs
composer.json
composer.lock
vendor
autoload.php
aws
google
So, everything Composer related is in a shared composer.json file, and all vendor files in the single vendor directory. But, I can't seem to find a way to just load up say AWS. It wants me to use the autoload.php from what I can tell, which seems to want to load up everything.
Do I need to set it up more like this if I want control over each library?
awscode.php
googlecode.php
libs
aws
composer.json
composer.lock
vendor
autoload.php
aws
google
composer.json
composer.lock
vendor
autoload.php
google
I'm obviously new to Composer and how to best utilize it, and want to make sure that I am setting it up the best way for both my situation, and for future management.
When using Composer, it only loads the classes when they are actually called in your code. To my knowledge this uses the PHP spl_autoload_register.
So in answer to your question, there won't be a significant extra overhead (if any).
Autoloading means that the file which defines a class gets read when you first use that class.
You should include all your project dependencies in one composer.json, they won't be loaded in files you don't use them in.

Categories