'; print_r($_SERVER); echo ''; exit; $client = new Google_Client(); $client->setAuthConfigFile('client_secrets.json'); $client->addScope(Google_Service_Analytics::ANALYTICS_READONLY); $client->setAccessType('offline'); $accessToken = file_exists($config['tokenFile']) ? file_get_contents($config['tokenFile']) : false; $action = empty($_GET['action']) ? 'index' : $_GET['action']; if ($accessToken) { $client->setAccessToken($accessToken); } set_exception_handler(function (Exception $e) { echo '

Exception

'; echo 'File: ' . $e->getFile() . ':' . $e->getLine() . '
'; echo 'Code: ' . $e->getCode() . '
'; echo 'Message: ' . $e->getMessage() . '
'; echo '
';
    echo $e->getTraceAsString();
    echo '
'; }); switch ($action) { case 'index': if (!$accessToken) { header('Location: ' . filter_var($config['redirectUri'], FILTER_SANITIZE_URL)); exit; } break; case 'authorize': $client->setRedirectUri($config['redirectUri']); if (!isset($_GET['code'])) { $authUrl = $client->createAuthUrl(); header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL)); exit; } else { $client->authenticate($_GET['code']); file_put_contents($config['tokenFile'], $client->getAccessToken()); header('Location: ' . filter_var($config['baseUri'], FILTER_SANITIZE_URL)); exit; } break; default: break; } if ($client->isAccessTokenExpired()) { //echo 'Token is expired ' . $client->getAccessToken() . '
'; //echo 'Refresh Token: ' . $client->getRefreshToken() . '
'; try { $client->refreshToken($client->getRefreshToken()); } catch (Google_Auth_Exception $e) { // Затичка. Якщо не вдалося оновити токен, то починаємо спочатку - авторизувати за участі юзера unlink($config['tokenFile']); header('Location: ' . filter_var($config['redirectUri'], FILTER_SANITIZE_URL)); exit; } file_put_contents($config['tokenFile'], $client->getAccessToken()); //echo 'New Token: ' . $client->getAccessToken() . '
'; } $content = file_get_contents(__DIR__ . '/basic-dashboard.html'); $placeholders = [ '{ids}' => 'ga:74406360', '{client_id}' => json_decode(file_get_contents('client_secrets.json'), true)['web']['client_id'], '{access_token}' => json_decode($client->getAccessToken(), true)['access_token'] ]; $content = str_replace(array_keys($placeholders), $placeholders, $content); die($content); $service = new Google_Service_Analytics($client); $result = $service->data_ga->get('ga:74406360', '2014-01-30', '2015-02-13', 'ga:sessions', [ 'dimensions' => 'ga:browser', 'sort' => '-ga:sessions', //'max-results' => 10 ]); echo '
';
print_r($result->getRows());
echo '
'; exit;