我正在构建一个使用 Google API PHP 客户端库的 Web 应用程序。该应用程序允许用户从 Google Analytic Accounts 检索分析数据。除了我将要解释的以下内容之外,一切都按其应有的方式工作。
第1步
用户在表单中选择一个帐户名称并从 accounts.php 页面提交表单。然后将该帐户的帐户 ID 保存在 SESSION 变量中。完成此操作后,accounts.php 页面使用位置标头重定向到 simple.php 页面。
第2步
simple.php 页面显示一个 Connect me 链接,然后用户按下该链接将其登录到 Google,以便从分析中检索数据。此数据检索所有帐户。然后,这些帐户通过一个针对帐户 id 会话变量的循环运行(以便检索用户在 accounts.php 页面中选择的正确帐户的结果)。
问题
问题是,当 Google 重定向回 simple.php 页面时,我之前存储在 accounts.php 中的所有会话数据都丢失了。即使我将帐户 ID 存储在 cookie 中。我认为 Google PHP 客户端库正在以某种方式在后端擦除我的会话数据。我尝试了很多想法,但似乎都不起作用。奇怪的是,如果我注销或返回 accounts.php 并重试,Session 变量会在 Google 重定向后保存其帐户 ID 数据。如此迷茫!
我希望我已经尽可能清楚地说明了这一点
<?php
session_start();
/*
if ($_SESSION['test'] != "logged" || $_SESSION['test'] == null) {
header('Location: http://www.xxxx.co.uk/');
die("Redirecting to home page");
}*/
header("Accept-Encoding: gzip");
header("User-Agent: gzip");
require_once 'src/apiClient.php';
require_once 'src/contrib/apiAnalyticsService.php';
$client = new apiClient();
$client -> setApplicationName("Google Analytics PHP Starter Application");
$client -> setClientId('xxxx');
$client -> setClientSecret('xxxx');
$client -> setRedirectUri('http://www.xxxx.co.uk/simple.php');
$client -> setDeveloperKey('xxxx');
$client -> setUseObjects(true);
$service = new apiAnalyticsService($client);
$table = array();
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()) {
$accounts = $service -> management_accounts -> listManagementAccounts(array("fields" => "items(id,name)"));
print "<a class='login' href='index.php?logout=true'>Logout</a>";
//$accounts = $service -> management_accounts -> listManagementAccounts();
foreach ($accounts->getItems() as $account) {
//var_dump($_SESSION['account_number']);
if ($account -> getId() == $_COOKIE["test"]) {
echo "</br>" . $account -> getName() . "</br>";
$props = $service -> management_webproperties -> listManagementWebproperties($account -> getId());
//foreach ($props->getItems() as $property) {
$profiles = $service -> management_profiles -> listManagementProfiles($account -> getId(), "~all");
foreach ($profiles -> getItems() as $profile) {
$counter++;
$table[$counter]['name'] = $profile -> getName();
echo "</br>" . $profile -> getName() . "</br>";
/*
echo "<h1>Profiles</h1>";
echo "<pre>";
var_dump($profile);
echo "</pre>";*/
// Direct
$data = $service -> data_ga -> get("ga:" . $profile -> getId(), $_SESSION['startdate'], $_SESSION['enddate'], 'ga:visits', array("segment" => "gaid::-7"));
// Non Paid
$data2 = $service -> data_ga -> get("ga:" . $profile -> getId(), $_SESSION['startdate'], $_SESSION['enddate'], 'ga:visits', array("segment" => "gaid::-5"));
// Paid
$data3 = $service -> data_ga -> get("ga:" . $profile -> getId(), $_SESSION['startdate'], $_SESSION['enddate'], 'ga:visits', array("segment" => "gaid::-4"));
// Refferal
$data4 = $service -> data_ga -> get("ga:" . $profile -> getId(), $_SESSION['startdate'], $_SESSION['enddate'], 'ga:visits', array("segment" => "gaid::-8"));
/*
echo "<h1>Non Paid Data</h1>";
echo "<pre>";
//var_dump($data);
echo "<pre>";
echo "<h1>Paid Data</h1>";
echo "</pre>";
//var_dump($data2);
echo "<pre>";
echo "<h1>Search Data</h1>";
echo "<pre>";
//var_dump($data3);
echo "</pre>";
echo "<h1>Referral Data</h1>";
echo "<pre>";
//var_dump($data4);
echo "</pre>";*/
$results1 = $data -> getTotalsForAllResults();
$results2 = $data2 -> getTotalsForAllResults();
$results3 = $data3 -> getTotalsForAllResults();
$results4 = $data4 -> getTotalsForAllResults();
foreach ($results1 as $result) {
echo "Direct Traffic " . $result . "<br/>";
}
foreach ($results2 as $result) {
$table[$counter]['nonpaid'] = $result;
echo "Non Paid Traffic " . $result . "<br/>";
}
foreach ($results3 as $result) {
echo "Paid Traffic " . $result . "<br/>";
}
foreach ($results4 as $result) {
$table[$counter]['refferal'] = $result;
echo "Refferal Traffic " . $result . "<br/>";
}
}
} else {
echo "This is not working";
var_dump($_COOKIE["test"]);
}
$_SESSION['token'] = $client -> getAccessToken();
}
echo "<table class='data'>";
foreach($table as $data) {
$total = $data['nonpaid'] + $data['refferal'];
echo "<tr><td>".$data['name']."</td><td>".$total."</td></tr>";
}
echo "</table>";
} else {
$authUrl = $client -> createAuthUrl();
var_dump($_COOKIE["test"]);
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
?>