Yii 1.1: jgoogleapi

JGoogleAPI it's an Yii extension to use the Google Api PHP Client in a easy way.

This extensions provides a wrapper to google-api-php-client library providing an easy way to access google apis and authentication. The extension is intended to enable any web application to access the Google Api with a service account or requiring the user authentication.


Yii 1.1.8 PHP >= 5.3


  • Copy

    • Copy files under extension folder


    • config/main.php
// application components
    'components' => array(
        'JGoogleAPI' => array(
            'class' => 'ext.JGoogleAPI.JGoogleAPI',
            //Default authentication type to be used by the extension
            //Account type Authentication data
            'serviceAPI' => array(
                'clientId' => 'YOUR_SERVICE_ACCOUNT_CLIENT_ID',
                'clientEmail' => 'YOUR_SERVICE_ACCOUNT_CLIENT_EMAIL',
                'keyFilePath' => 'THE_PATH_TO_YOUR_KEY_FILE',
            //You can define one of the authentication types or both (for a Service Account or Web Application Account) 
            webAppAPI = array(
                'clientId' => 'YOUR_WEB_APPLICATION_CLIENT_ID',
                'clientEmail' => 'YOUR_WEB_APPLICATION_CLIENT_EMAIL',
                'clientSecret' => 'YOUR_WEB_APPLICATION_CLIENT_SECRET',
                'redirectUri' => 'YOUR_WEB_APPLICATION_REDIRECT_URI',
                'javascriptOrigins' => 'YOUR_WEB_APPLICATION_JAVASCRIPT_ORIGINS',
            'simpleApiKey' => 'YOUR_SIMPLE_API_KEY',
            //Scopes needed to access the API data defined by authentication type
            'scopes' => array(
                'serviceAPI' => array(
                    'drive' => array(
                'webappAPI' => array(
                    'drive' => array(
            //Use objects when retriving data from api if true or an array if false


    • Create a service
$service = Yii::app()->JGoogleAPI->getService('Drive');
    $service = Yii::app()->JGoogleAPI->getService('Drive','webappAPI');
    //if the authentication type is diferent of the default
    • Create a object
$file = Yii::app()->JGoogleAPI->getObject('DriveFile','Drive');
    //we pass the object name that we want to create and the service where it belongs
    $file = Yii::app()->JGoogleAPI->getObject('DriveFile','Drive','webappAPI');
    //if the authentication type is different of the default

If you choose 'webappAPI' authentication method you need aditional steps, because you need to save the token from the authenticate method

    API Ex: http://code.google.com/p/google-api-php-client/

//Create an extension Instance
    $jgoogleapi = Yii::app()->JGoogleAPI;
    try {
        if(!isset(Yii::app()->session['auth_token'])) {
            //Get the instance of the client from the api
            $client = $jgoogleapi->getClient();
            //$client = Yii::app()->JGoogleAPI->getClient();    #Without creating an extension instance            
            //Web Application User authentication
            //You want to use a persistence layer like the DB or memcached to store the token for the current user
        } else {
            //List files from Google Drive
            $files = $jgoogleapi->getService('Drive')->files->listFiles();
            //Check the api documentation to see other ways to interact with api
            // We're not done yet. Remember to update the cached access token.
            // Remember to replace $_SESSION with a real database or memcached.
            Yii::app()->session['auth_token'] = $client->getAccessToken();
    }catch(Exception $exc) {
        //Becarefull because the Exception you catch may not be from invalid token
        throw $exc;


This extension uses google-api-php-lib-0.6.2


When upgrading this extension check the documentation of Google API, the updated version works like the previkous one, but the google-api-client-php has some diferences from previous version.

Total 3 comments

#13376 report it
mishamx at 2013/05/26 11:36am
RE: Google analytics?

@aleksdj very esy


'scopes'=> array(

Use analytics api

$service = Yii::app()->JGoogleAPI->getService('analytics');
$results = $service->data_ga->get(
        'dimensions'    => 'ga:source,ga:date',
        'sort'          => 'ga:date,-ga:visits',
        'filters'       => 'ga:visits>100',
        'max-results'   => '100'
// you can use Google Analytics Query Explorer 2: http://ga-dev-tools.appspot.com/explorer/
#10941 report it
n3okill at 2012/12/05 10:52am
Google Analytics

Hello, this extension is useful to work with the Google API, this is a wrapper to the google-api-php-lib-client, and it can do anything that lib does, i'm almost sure it can show the information that you need from analytics, i just don't know how you can do it because i don't have explored the analytics api, but the authentication process is just like in the example. You can see this Analytics Example for one example of implementation and check the Analytics Api Documentation, they also have Hello Analytics API Tutorial where you can see more one example. Just don't forget to define the right scopes that you need to use, you can find it in the api documentaion. Sorry for not giving one example but i really don't have time now to investigate the analytics api. Hope the information helps, and please let me know if you need some assistance with the extension.

#10939 report it
aleksdj at 2012/12/05 08:23am
Google analytics?

Hello, is this useful for displaying Google Analytics data on our platform dashboard? Could you do an example? thanks in advance

Leave a comment

Please to leave your comment.

Create extension