php – 使用HelloAnalyticsApi时,在Google Analytics中调用未定义的方法Google_Client
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 使用HelloAnalyticsApi时,在Google Analytics中调用未定义的方法Google_Client,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3083字,纯文字阅读大概需要5分钟。
内容图文
我正在使用Core Reporting API进行报告.我在我的localhost服务器上安装了Google PHP API客户端主服务器,并在src文件夹中创建了一个文件HelloAnalyticsAPi.php
Google/Client.php
,
Google/Service/Analytics.php
文件.并使用以下详细信息
$client->setClientId('XXXXXXXXXXX.apps.googleusercontent.com');
$client->setClientSecret('XXXXXXXXXXX');
$client->setRedirectUri('http://localhost/analytics/src/HelloAnalyticsApi.php');
$client->setDeveloperKey('XXXXXXXXXXX');
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
$client->setUseObjects(true);
我在setUseObjects上有致命错误.错误是致命错误:调用未定义的方法Google_Client :: setUseObjects().我也在谷歌分析后端做了一些授权.
请告诉我在我的服务器上获取报告的整个过程.因为我无法理解他们给出的谷歌分析的开发者指南.
解决方法:
您遇到的问题是您有错误的客户端库. Hello Analytics API教程是使用Code.google – google-api-php-client 上的旧库创建的,而不是github上的新版本.
更新:
由于教程仍未更新,我已经制作了可能有用的教程. Google Oauth2 php.以下代码直接从中删除.本教程将保持最新,您可能需要检查是否有任何更改.
<?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'];
?>
内容总结
以上是互联网集市为您收集整理的php – 使用HelloAnalyticsApi时,在Google Analytics中调用未定义的方法Google_Client全部内容,希望文章能够帮你解决php – 使用HelloAnalyticsApi时,在Google Analytics中调用未定义的方法Google_Client所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。