M
M
mannerheim2015-03-25 19:01:13
PHP
mannerheim, 2015-03-25 19:01:13

How to make a simple php script to work with Google Analytics API?

Hello!
Help to run the simplest script for working with the Google Analytics API.
What I do:
1. Downloaded the archive from https://github.com/google/google-api-php-client/
2. Expanded it to the test/google-api-php-client-master/ directory
3. Created the file test/ index.php with this code:

<?php
require_once('google-api-php-client-master/src/Google/Client.php');
require_once('google-api-php-client-master/src/Google/Service/Analytics.php');
?>

When I run it, I immediately get:
Fatal error: Class 'Google_Service' not found in X:\home\foo\www\test\google-api-php-client-master\src\Google\Service\Analytics.php on line 32

I don't understand how to work. I saw two approaches on the net: people either include these two files, or include the Google/autoload.php file. The second variant does not work for me at all - the Apache falls.
There is a standard Denwer (last), php 5.3.13
UPD :
Here is the code that brings down Apache on a Windows machine, but works on Linux.
<?php
require_once('google-api-php-client-master/src/Google/autoload.php');

define ('APP_NAME', 'Analytics Example');
define ('APP_EMAIL', '650831266*********@developer.gserviceaccount.com');
define ('PATH_TO_PRIVATE_KEY_FILE', 'API Project-7d819********.p12');
define ('CLIENT_ID', '********.apps.googleusercontent.com');

$client = new Google_Client();
$client->setApplicationName(APP_NAME);
$client->setClientId(CLIENT_ID); 
$client->setAccessType( 'offline_access');

$client->setAssertionCredentials(
  new Google_Auth_AssertionCredentials(
    APP_EMAIL, 
    array('https://www.googleapis.com/auth/analytics.readonly'),
    file_get_contents(PATH_TO_PRIVATE_KEY_FILE)  
  )
); 
 
$service = new Google_Service_Analytics($client);

//print_r($service);

$data = $service->data_ga->get(
    'ga:***',
    '2015-03-26',
    '2015-03-26',
    'ga:users'
    );
 
print_r($data);
?>

And for some reason the answer comes 0
[totalsForAllResults] => Array
        (
            [ga:users] => 0
        )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max, 2015-03-25
@AloneCoder

Try set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/google-api-php-client/src');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question