Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //include_once "templates/base.php";
- session_start();
- require_once '../../vendor/autoload.php';
- $client_id = '**************************************.apps.googleusercontent.com';
- $client_secret = '******************';
- $redirect_uri = 'http://127.0.0.1';
- $client = new Google_Client();
- $client->setClientId($client_id);
- $client->setClientSecret($client_secret);
- $client->setRedirectUri($redirect_uri);
- $client->addScope("https://www.googleapis.com/auth/webmasters");
- if (isset($_REQUEST['logout'])) {
- unset($_SESSION['access_token']);
- }
- if (isset($_GET['code'])) {
- $client->authenticate($_GET['code']);
- $_SESSION['access_token'] = $client->getAccessToken();
- $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
- header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
- }
- if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
- $client->setAccessToken($_SESSION['access_token']);
- } else {
- $authUrl = $client->createAuthUrl();
- }
- if ($client->getAccessToken()) {
- $_SESSION['access_token'] = $client->getAccessToken();
- $q = new \Google_Service_Webmasters_SearchAnalyticsQueryRequest();
- $q->setStartDate('2015-09-01');
- $q->setEndDate('2015-09-01');
- $q->setDimensions(['page']);
- $q->setSearchType('web');
- try {
- $service = new Google_Service_Webmasters($client);
- $u = $service->searchanalytics->query('http://sschesnok.com.ua', $q);
- echo '<table border=1>';
- echo '<tr>
- <th>#</th><th>Clicks</th><th>CTR</th><th>Imp</th><th>Page</th><th>Avg. pos</th>';
- for ($i = 0; $i < count($u->rows); $i++) {
- echo "<tr><td>$i</td>";
- echo "<td>{$u->rows[$i]->clicks}</td>";
- echo "<td>{$u->rows[$i]->ctr}</td>";
- echo "<td>{$u->rows[$i]->impressions}</td>";
- echo "<td>{$u->rows[$i]->keys[0]}</td>";
- echo "<td>{$u->rows[$i]->position}</td>";
- /* foreach ($u->rows[$i] as $k => $value) {
- //this loop does not work (?)
- } */
- echo "</tr>";
- }
- echo '</table>';
- } catch(\Exception $e ) {
- echo $e->getMessage();
- }
- }
- ?>
- <div class="request">
- <?php
- if (isset($authUrl)) {
- echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>";
- } else {
- echo <<<END
- <form id="url" method="GET" action="{$_SERVER['PHP_SELF']}">
- <input name="url" class="url" type="text">
- <input type="submit" value="Shorten">
- </form>
- <a class='logout' href='?logout'>Logout</a>
- END;
- }
- ?>
- </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement