2015-04-19 59 views
2

我试图从我的谷歌分析帐户在普通网页上显示简单的查询(总视图)。 我把这样的代码:PHP谷歌分析API - 简单查询

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>'; 

在我写的私人数据###。我不知道为什么它不起作用。 的输出是: 警告:在非调用一个成员函数的get():file_get_contents()函数[function.file-GET-内容]:文件名不能为空在path.php第19行

致命错误-object在path.php上线39

任何人都可以帮助我吗?

+1

'$ key_file_location?'没有被定义,你在哪里定义它? – ghanbari

+0

那是什么?带有一些秘密数据的额外文件,如变量$ client? – Marcin

+0

此外,在调用它之前,是否需要实际创建分析服务对象?我没有看到你在从它调用方法之前声明/实例化$ service。 –

回答

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 

有任何类似的问题?