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

  1. Where should i put the library?
  2. Where should i declare the library?
  3. Should i include something like use

Thanks in advance

1 Answer 1


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.


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.

  • Thank you very much. Then I should wait until i migrate to a vps, now im on shared hosting and i can't use the console. Is it possible with the composer.json file? How it should look like? Sep 13, 2016 at 21:39
  • composer is not the same as the DrupalConsole project. You can run this stuff in your local environment, commit the vendor directory and push that to your VPS. No need to run it remotely. If you do not have a local environment, you might want to look at setting up Acquia Dev Desktop which might make things easier than trying to work remotely all the time.
    – Kevin
    Sep 13, 2016 at 21:40
  • Adding a composer.json file to your module won't have any effect when you push/upload that to your server - the command must be run to fetch the package. I was just saying that you could bundle it all together later for other users if you were to release this module (as example). I don't want to get you into the weeds too deep, this should solve the problem at hand first.
    – Kevin
    Sep 13, 2016 at 21:43
  • Thank you . I'm downloading awesome Aquia Dv Desktop, didn't know about it. I will try and give you feedback soon. :) Sep 13, 2016 at 21:51
  • You will also need to install composer, which is why I linked to the install instructions.
    – Kevin
    Sep 13, 2016 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.