Search code examples
yii2google-contacts-api

Google Contacts API with Yii2


I am trying to use the Google contacts api from within Yii2.

Google provides a composer interface, so I added

"google/apiclient": "1.0.*@beta"

to my composer.json and did "composer update"

Now - how do I actually use this from within my Yii2 controller?

I have a working, standalone test script that works. In that, it does

require_once 'GoogleClientApi/autoload.php';
$client = new Google_Client();

However, this does not seem to work from Yii.

I've tried adding the require to my index.php file, as follows (copying autoload.php from a previous installation since autoload doesn't seem to come with the composer version - why would this be omitted?)

require(__DIR__ . '/../vendor/google/apiclient/autoload.php');

but this results in the following error when called in my controller:

Call to undefined function app\controllers\Google_Client()

I've read the post on http://www.yiiframework.com/doc-2.0/guide-tutorial-yii-integration.html but this doesn't make much sense to me.

Any help appreciated. Thanks


Solution

  • Your code is almost correct:

    require_once 'GoogleClientApi/autoload.php';
    $client = new \Google_Client();
    

    The "\" is needed to indicate the class exists in the global namespace, as you are - in that piece of code - under the app\controllers namespace.