Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.62 KB | None | 0 0
  1. <?php
  2. //include_once "templates/base.php";
  3. session_start();
  4.  
  5. require_once '../../vendor/autoload.php';
  6.  
  7. $client_id = '**************************************.apps.googleusercontent.com';
  8. $client_secret = '******************';
  9. $redirect_uri = 'http://127.0.0.1';
  10.  
  11. $client = new Google_Client();
  12. $client->setClientId($client_id);
  13. $client->setClientSecret($client_secret);
  14. $client->setRedirectUri($redirect_uri);
  15. $client->addScope("https://www.googleapis.com/auth/webmasters");
  16.  
  17. if (isset($_REQUEST['logout'])) {
  18.   unset($_SESSION['access_token']);
  19. }
  20.  
  21. if (isset($_GET['code'])) {
  22.   $client->authenticate($_GET['code']);
  23.   $_SESSION['access_token'] = $client->getAccessToken();
  24.   $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  25.   header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
  26. }
  27.  
  28. if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  29.   $client->setAccessToken($_SESSION['access_token']);
  30. } else {
  31.   $authUrl = $client->createAuthUrl();
  32. }
  33.  
  34. if ($client->getAccessToken()) {
  35.   $_SESSION['access_token'] = $client->getAccessToken();
  36.  
  37.     $q = new \Google_Service_Webmasters_SearchAnalyticsQueryRequest();
  38.  
  39.     $q->setStartDate('2015-09-01');
  40.     $q->setEndDate('2015-09-01');
  41.     $q->setDimensions(['page']);
  42.     $q->setSearchType('web');
  43.     try {
  44.        $service = new Google_Service_Webmasters($client);
  45.        $u = $service->searchanalytics->query('http://sschesnok.com.ua', $q);
  46.        echo '<table border=1>';
  47.        echo '<tr>
  48.          <th>#</th><th>Clicks</th><th>CTR</th><th>Imp</th><th>Page</th><th>Avg. pos</th>';
  49.           for ($i = 0; $i < count($u->rows); $i++) {
  50.             echo "<tr><td>$i</td>";
  51.             echo "<td>{$u->rows[$i]->clicks}</td>";
  52.             echo "<td>{$u->rows[$i]->ctr}</td>";
  53.             echo "<td>{$u->rows[$i]->impressions}</td>";
  54.             echo "<td>{$u->rows[$i]->keys[0]}</td>";
  55.             echo "<td>{$u->rows[$i]->position}</td>";
  56.  
  57.             /* foreach ($u->rows[$i] as $k => $value) {
  58.                 //this loop does not work (?)
  59.             } */
  60.             echo "</tr>";
  61.           }            
  62.         echo '</table>';
  63.      } catch(\Exception $e ) {
  64.         echo $e->getMessage();
  65.      }  
  66. }
  67. ?>
  68. <div class="request">
  69. <?php
  70.     if (isset($authUrl)) {
  71.       echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>";
  72.     } else {
  73.       echo <<<END
  74.      <form id="url" method="GET" action="{$_SERVER['PHP_SELF']}">
  75.        <input name="url" class="url" type="text">
  76.        <input type="submit" value="Shorten">
  77.      </form>
  78.      <a class='logout' href='?logout'>Logout</a>
  79. END;
  80. }
  81. ?>
  82. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement