带有PHP的Google Analytics API V3返回空白页

带有PHP的Google Analytics API V3返回空白页,php,google-analytics,Php,Google Analytics,我正在尝试使用Google Analytics Reporting API和Oauth 2.0显示有关我网站的一些基本Google Analytics信息。我已经试了好几个小时,但是没有成功 我已经从中复制并编辑了代码。一切都应该正常工作,但不知何故,当我在浏览器中查看页面时,我只看到一个空白页面 我有: 在Google API控制台中创建了一个服务帐户。 在Google API控制台中启用Google Analytics API。 将服务帐户的电子邮件地址添加到Google Analytics

我正在尝试使用Google Analytics Reporting API和Oauth 2.0显示有关我网站的一些基本Google Analytics信息。我已经试了好几个小时,但是没有成功

我已经从中复制并编辑了代码。一切都应该正常工作,但不知何故,当我在浏览器中查看页面时,我只看到一个空白页面

我有:

在Google API控制台中创建了一个服务帐户。 在Google API控制台中启用Google Analytics API。 将服务帐户的电子邮件地址添加到Google Analytics。 代码“XXXX”表示我已过滤掉个人信息:

<?php

require_once 'google-api/src/Google/Client.php';
require_once 'google-api/src/Google/Service/Analytics.php';

$keyfile = 'XXXX.p12';

// Initialise the Google Client object
$client = new Google_Client();
$client->setApplicationName('Application Name');

$client->setAssertionCredentials(
    new Google_AssertionCredentials(
        'XXXX@developer.gserviceaccount.com',
        array('https://www.googleapis.com/auth/analytics.readonly'),
        file_get_contents($keyfile)
    )
);

// Get this from the Google Console, API Access page
$client->setClientId('XXXX.apps.googleusercontent.com');
$client->setAccessType('offline_access');
$analytics = new Google_AnalyticsService($client);

// We have finished setting up the connection,
// now get some data and output the number of visits this week.

// Your analytics profile id. (Admin -> Profile Settings -> Profile ID)
$analytics_id   = 'XXXX';
$lastWeek       = date('Y-m-d', strtotime('-1 week'));
$today          = date('Y-m-d');

try {
    $results = $analytics->data_ga->get($analytics_id,
                        $lastWeek,
                        $today,'ga:visits');
    echo '<b>Number of visits this week:</b> ';
    echo $results['totalsForAllResults']['ga:visits'];
} catch(Exception $e) {
    echo 'There was an error : - ' . $e->getMessage();
}

?>

我已经为此奋斗了很长时间,任何帮助都将不胜感激

•将服务帐户的电子邮件地址添加到Google Analytics@DaImTo是的,在账户层面。奇怪的是,你的代码对我有用,谢谢你!:-,但是我的原始帖子中的代码没有。是的,原始版本对我来说很奇怪,我想知道它是否使用了旧版本的客户端库。我认为最好的开始是:。正如@DalmTo所提到的,它可能是库版本。@carlodurso谢谢,但那是我使用的版本。也许我引用了错误的文件?