Attaching Google PHP Library to custom module How do I use an external PHP package in my own module?

This is the one I want to use: https://github.com/googleads/googleads-php-lib

I have some questions


Where should i put the library? 
Where should i declare the library?
Should i include something like use


Thanks in advance
I advise using composer to do this.

First, you need to pull in the library:

composer require googleads/googleads-php-lib:dev-experimental


The reason you want the experimental branch is it has added PSR-4 support and namespaces to its various classes. The master branch does not have these changes.

Then, to use any of the classes in your own class files, you need to use use statements.

use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201605\cm\OrderBy;
use Google\AdsApi\AdWords\v201605\cm\Paging;
use Google\AdsApi\AdWords\v201605\cm\Selector;
use Google\AdsApi\Common\OAuth2TokenBuilder;

class MyController extends ControllerBase {

  // code...

  public function foo($args...) {
    $adWordsServices = new AdWordsServices();
    // code...
  }

}


Check the README in that branch (not the master) for more examples.

https://github.com/googleads/googleads-php-lib/tree/experimental

composer require (run it from the root of the application) will add this package to the list of dependencies.

If you are shaping this module to someday be a contributed module on drupal.org, you can add your own composer.json file to the custom module folder, and add this Google package as a dependency of the module. When users run composer require drupal/drupal_rollers_module - it will automatically fetch the Google library as well for them.

One last thing, the example above is kinda not so great (instantiating a new object in a method) and what you really want to do is have dependency injection provide that for you. I haven't taken a deeper look at the Google library you want, but you would need to define some services that pull in those classes in order to get to that point. Feel free to open another question later or peruse the ones already discussing DI.