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
任何人都可以帮助我吗?
'$ key_file_location?'没有被定义,你在哪里定义它? – ghanbari
那是什么?带有一些秘密数据的额外文件,如变量$ client? – Marcin
此外,在调用它之前,是否需要实际创建分析服务对象?我没有看到你在从它调用方法之前声明/实例化$ service。 –