谷歌Analytics(分析)API包括路径
我试图设置在我的代码使用的是什么谷歌已经提供包括动态路径: set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/google-api-php-client/src');
谷歌Analytics(分析)API包括路径
我还是比较入门到PHP,我会承担/path/to/
需要被改成别的东西。我曾尝试将其设置为/home/expiredf/public_html/google-api-php-client/src
但这不起作用。
有人可以帮助我通过这个HelloAnalyticsAPI.php教程?
还收到任何链接的答案其他问题,我已经完成大部分的看着,并试图实现自己的解决方案,但他们没有工作对我来说,他们似乎与PHP记较高的专业知识来解释。基本和愚蠢的答案表示赞赏。
您的问题的最佳答案是说不要使用该教程。该教程极其过时并使用旧的PHP-client-Lib,这已报告给Google。
PHP客户端库
的谷歌PHP客户端库的最新版本可以在Github上找到。 google-api-php-client每当有任何更改时,这个客户端库会定期更新。您需要将整个src/Google目录复制到您的应用程序的目录中。除非你真的知道你在做什么,否则我不建议只采取你需要的文件。
的oauth2
有3个步骤的oauth2这就是为什么它被称为3条腿的认证。在第一步中,您要求用户为您提供访问权限,第二步是用户为您提供访问权限,第三步和最后一步是您交换用户访问数据的访问权限。
<?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("{devkey}");
$client->setClientId('{clientid}.apps.googleusercontent.com');
$client->setClientSecret('{clientsecret}');
$client->setRedirectUri('http://www.daimto.com/Tutorials/PHP/Oauth2.php');
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
//For loging out.
if ($_GET['logout'] == "1") {
unset($_SESSION['token']);
}
// Step 2: The user accepted your access now you need to exchange it.
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
// Step 1: The user has not authenticated we give them a link to login
if (!$client->getAccessToken() && !isset($_SESSION['token'])) {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
// Step 3: We have access we can now create our service
if (isset($_SESSION['token'])) {
print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>";
$client->setAccessToken($_SESSION['token']);
$service = new Google_Service_Analytics($client);
// request user accounts
$accounts = $service->management_accountSummaries->listManagementAccountSummaries();
foreach ($accounts->getItems() as $item) {
echo "Account: ",$item['name'], " " , $item['id'], "<br /> \n";
foreach($item->getWebProperties() as $wp) {
echo ' WebProperty: ' ,$wp['name'], " " , $wp['id'], "<br /> \n";
$views = $wp->getProfiles();
if (!is_null($views)) {
foreach($wp->getProfiles() as $view) {
// echo ' View: ' ,$view['name'], " " , $view['id'], "<br /> \n";
}
}
}
} // closes account summaries
}
print "<br><br><br>";
print "Access from google: " . $_SESSION['token'];
?>
此代码是直接从我的教程撕开:Google Oauth2 PHP - Google Analytics API的教程保持最新。如果这篇文章比较陈旧,你可能想看看代码中是否有任何改变。
还没有实现任何代码,但事实上,你已经指出我关于教程正确的方向,我已经标记为正确的:) – TheAngryBr1t 2014-10-06 23:10:08
现在实施和伟大的工作,得到正是我一直在试图让,现在只是开始阅读更多的进入数据。谢谢 :) – TheAngryBr1t 2014-10-13 22:09:51
该教程是日期的出路,试试这个:http://www.daimto.com/google-oauth2-php/ – DaImTo 2014-10-01 06:17:42