2

I am developing a php module that uses the Google Client class from the google client php api. I have downloaded and unzipped the latest google client php api at sites/all/libraries, and put my module at sites/all/modules. The code loading the library is this:

if (($library = libraries_load("google-api-php-client")) && !empty($library['loaded'])) {
    print "success";
}

$client = new Google_Client();

However, I am getting "Class 'Google_Client' not found" when I try to access it. Also, the print is not showing, as if the library never loads. However when I use libraries_get_path() I do receive the correct path: sites/all/libraries/google-api-php-client

I have tried the checks here but they all fail. I am using the Libraries API 7.x-2.1 and drupal 7.22

My hook_libraries_info() implementation:

function hook_libraries_info() {
    $libraries['google-api-php-client'] = array(
        'name' => 'Google API PHP Client', 
        'vendor url' => 'https://code.google.com/p/google-api-php-client/', 
        'download url' => 'https://code.google.com/p/google-api-php-client/downloads/list', 
        'version arguments' => array(
            'file' => 'README',
            'pattern' => '/Version (\d+)/', 
            'lines' => 5,
        ), 
        'files' => array(
            'php' => array(
                'Google_Client.php',
                'config.php',
            ),
        ),
        'integration files' => array(
            'calendarVis' => array(
                'php' => array(
                    'Google_Client.php',
                    'config.php',
                ),
            ),
        ),
    );
    return $libraries;
}

Other than that, I have tried creating an .info file, which worked on my initial wampserver setup, but I cannot make it work anywhere else. Here is the .info file:

name = calendarVis
description = Google Calendar Visualization for P-space
core = 7.x

dependencies[] = libraries

Also, after accessing the page that uses the module and returning to administrator menus, I am getting this error message:

"Warning: Invalid argument supplied for foreach() in libraries_invoke() (line 161 of drupal-7.22/sites/all/modules/libraries/libraries.module). Notice: Undefined index: installed in libraries_load() (line 621 of drupal-7.22/sites/all/modules/libraries/libraries.module)."

  • Could you post your hook_libraries_info() implementation? – Clive Jul 3 '13 at 11:21
  • I already had an .info file, I added a hook_libraries_info() implementation later but no results. – Mike Drakoulelis Jul 3 '13 at 12:15
  • Since the libraries info data is cached, did you clear your cache after implementing hook_libraries_info()? – 4life Jul 3 '13 at 12:24
  • Yes, but no changes. I have added some error messages that appear after accessing the module page. – Mike Drakoulelis Jul 3 '13 at 12:32
1

Here's an updated answer (the version of the client library I'm using is "google/apiclient": "1.0.*@beta")

    function hook_libraries_info() {
     $libraries['google-api-php-client'] = array(
     'name' => 'Google API PHP Client',
     'vendor url' => 'http://developers.google.com/api-client-library/php',
     'download url' => 'http://developers.google.com/api-client-library/php',
     'path' => 'src/Google/',
     'version' => '1',
     'files' => array(
      'php' => array(
        'autoload.php'
      )
    )
  );
  return $libraries;
}

Note, the 'autoload.php' file in the files array. This will automagically load all the classes you might want, and seems to work well for me; rather than manually selecting each file the library includes that I might need.

I load the library and then use it like this:

$library = libraries_load('google-api-php-client');
module_load_include('inc', 'page_speed', 'page_speed_client');

My inc file contains the logic I want for defining a new client and service (see the Google docs) and does a Pagespeed Insight query, and (for now) simply dumps the returned data onto a page.

Final caveat, remember to clear cache if you edit your files array, otherwise the library won't load properly, that caught me out a couple of times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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