PHP Google AnalyticsAPI - 简单查询

时间:2015-04-19 08:08:59

标签: php google-analytics google-analytics-api

我尝试在常用网页上显示来自我的Google Analytics帐户的简单查询(总浏览量)。 我把这样的代码:

require_once 'Google/autoload.php';
require_once 'Google/Client.php';     
require_once 'Google/Service/Analytics.php';       
//session_start();      
$client = new Google_Client();
    $client->setApplicationName("Client_Library_Examples");
    $client->setDeveloperKey("###");  
    $client->setClientId('####.apps.googleusercontent.com');
    $client->setClientSecret('###');
    $client->setRedirectUri('###');
    $client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));



  //first authentication

$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array('https://www.googleapis.com/auth/analytics.readonly'),
    $key
);
$client->setAssertionCredentials($cred);

//first call

$ids = 'ga:###'; //your id
$startDate = '2013-12-25';
$endDate = '2014-01-08';
$metrics = 'ga:transactions';

$optParams = array(
    'dimensions' => 'ga:campaign',
    'max-results' => '10'
);

$results = $service->data_ga->get($ids, $startDate, $endDate, $metrics, $optParams);

//Dump results
echo "<h3>Results Of Call:</h3>";

echo "dump of results";
var_dump($results);

echo "results['totalsForAllResults']";
var_dump($results['totalsForAllResults']);

echo "results['rows']";
foreach ($results['rows'] as $item) {
    var_dump($item);
}

//second call

$results = $service->data_ga;

echo '<pre>';
print_r($results);
echo '</pre>';

在私人数据中,我写了###。我不知道它为什么不起作用。 输出是: 警告:file_get_contents()[function.file-get-contents]:第19行的path.php中的文件名不能为空

致命错误:在第39行的path.php中调用非对象的成员函数get()

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

好的,找到私钥位置:) 但仍然在页面上进行沟通:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /###/index.php:1) in /home/users/marcwars/public_html/wpress/index.php on line 3

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /###/index.php:1) in /home/users/marcwars/public_html/wpress/index.php on line 3

Fatal error: Class 'Google_Service' not found in /###/Google/Service/Analytics.php on line 32

有没有类似的问题?