2015-04-19 73 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 

有任何類似的問題?