Adam Adam - 1 month ago 22
PHP Question

Integrate Google API's Analytics Client Library PHP into Symfony2

I have successfully followed through the following Google article, https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/service-php and setup a basic php script for extracting Analytics data. It was quite easy :-)

I am now trying to integrate this code into my Symfony2 project. Now it seems that the library is written in an ancient style, no namespacing and filenames are different to that of the class in them. Can anyone help me on how I can integrate it into my symfony project? I have loaded in the git repository via composer with the follwoing:

"repositories": [
{
"type": "package",
"package": {
"name": "google/google-api-php-client",
"version": "1.1.4",
"dist": {
"url": "https://github.com/google/google-api-php-client/archive/1.1.4.zip",
"type": "zip",
"reference": "1.1.4"
}
}
}
],


But the issue is all of the autoloading etc.

Thanks in advance!

Answer

The library has a composer.json file and is registered in packagist, so you just need to add "google/apiclient": "1.1.4" in your composer's require section, and composer will handle the autoloading.

Then to integrate it to Symfony the right way, please read the doc here to learn how to register your own services in your services.yml file.

Define your application name and your key as parameters in your parameters.yml to reuse them in your service definitions.

Define a service for each class you instanciate (e.g. a service for Google_Client, Google_Service_Analytics, Google_Auth_AssertionCredentials, ...) with the right dependencies on other services and/or parameters.

Note: I've found those bundle on knpbundles: isometriks/GoogleApiBundle and Happyr/GoogleApiBundle, they may help.