我正在尝试使用php与GA API v3进行交互。作为一个相当新的PHP,我有点挣扎。有没有人有任何使用api与php(v3)的经验?
http://code.google.com/intl/nl/apis/analytics/docs/index.html
Google确实提供了一个小样本脚本,但它实际上没用(我的技能有限)因为它返回了一个api密钥,但没有告诉你它需要去哪里或为什么需要它。
如果有任何人知道,如果你能告诉我如何,我将非常感激。
答案 0 :(得分:3)
您需要确保通过其API控制台向Google注册API。确保您启用Google Analytics,然后创建一个项目。
确保从Google Code下载完整的api。
您想要进入位于Google Analytics文件夹中的simple.php(在示例下)并取消注释第11-14行,并使用Google API控制台中的信息替换:
$client->setClientId('xxxx.apps.googleusercontent.com');
$client->setClientSecret('xxxxxx');
$client->setRedirectUri('http://www.xxxx.com/xxx/examples/analytics/simple.php');
$client->setDeveloperKey('xxxxxxxxxxx');
这将让您连接,您将看到基本数据。有关详细信息和精彩教程,您可以看到它here。
您的整个页面应如下所示:
<?php
require_once '../../src/apiClient.php';
require_once '../../src/contrib/apiAnalyticsService.php';
session_start();
$client = new apiClient();
$client->setApplicationName("Google Analytics PHP Starter Application");
// Visit https://code.google.com/apis/console?api=analytics to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('addyourshere');
$client->setClientSecret('addyourshere');
$client->setRedirectUri('addyourshere');
$client->setDeveloperKey('addyourshere');
$service = new apiAnalyticsService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
$props = $service->management_webproperties->listManagementWebproperties("~all");
print "<h1>Web Properties</h1><pre>" . print_r($props, true) . "</pre>";
$accounts = $service->management_accounts->listManagementAccounts();
print "<h1>Accounts</h1><pre>" . print_r($accounts, true) . "</pre>";
$segments = $service->management_segments->listManagementSegments();
print "<h1>Segments</h1><pre>" . print_r($segments, true) . "</pre>";
$goals = $service->management_goals->listManagementGoals("~all", "~all", "~all");
print "<h1>Segments</h1><pre>" . print_r($goals, true) . "</pre>";
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}