gpt4 book ai didi

php - 使用 HelloAnalyticsApi 时调用 Google Analytics 中未定义的方法 Google_Client

转载 作者:可可西里 更新时间:2023-11-01 00:09:11 25 4
gpt4 key购买 nike

我正在使用核心报告 API 进行报告。我在我的本地主机服务器上安装了 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 上遇到 fatal error 。错误是 fatal error :调用未定义的方法 Google_Client::setUseObjects()。我也在我的谷歌分析后端做了一些授权。

请让我知道在我的服务器上获取报告的整个过程。因为我无法理解他们提供的 Google Analytics 开发人员指南。


最佳答案

您遇到的问题是您的客户端库有误。 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 '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WebProperty: ' ,$wp['name'], " " , $wp['id'], "<br /> \n";

$views = $wp->getProfiles();
if (!is_null($views)) {
foreach($wp->getProfiles() as $view) {
// echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22370951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com