title: Is there any possible to get Google Webmaster Tool (GWT) datas using PHP?
body: <p>I'm trying to get some datas from <strong>Google Webmaster Tool (GWT)</strong>, I have searched some of the API Documents and Implements, But they are returning few of the datas only from the GWT. </p>
  
  <p><em><strong>My Needs :</em></strong> </p>
  
  <p>Needs to get the datas of the following from GWT,</p>
  
  <p>(1). TOP_PAGES</p>
  
  <p>(2). TOP_QUERIES</p>
  
  <p>(3). CRAWL_ERRORS</p>
  
  <p>(4). CONTENT_ERRORS</p>
  
  <p>(5). CONTENT_KEYWORDS</p>
  
  <p>(6). INTERNAL_LINKS</p>
  
  <p>(7). EXTERNAL_LINKS</p>
  
  <p>(8). SOCIAL_ACTIVITY</p>
  
  <p>After getting these datas, i need to generate the Excel file for each of them.</p>
  
  <p><em><strong>Achieved :</em></strong></p>
  
  <p>I have got few datas from the above and generated into the Excel file.such as,</p>
  
  <p>(1). TOP_PAGES</p>
  
  <p>(2). TOP_QUERIES</p>
  
  <p>(3). INTERNAL_LINKS</p>
  
  <p>(4). EXTERNAL_LINKS</p>
  
  <p>(5). CONTENT_KEYWORDS</p>
  
  <p><em><strong>Not Achieved :</em></strong></p>
  
  <p>Still I'm not getting the major parts / datas like,</p>
  
  <p>(1). CRAWL_ERRORS</p>
  
  <p>(2). CONTENT_ERRORS</p>
  
  <p>(3). SOCIAL_ACTIVITY</p>
  
  <p><em><strong>Code Samples For Your Reference :</em></strong></p>
  
  <p>I have used two files in PHP for this GWT API, </p>
  
  <p><em><strong>File #1 :</em></strong> ( gwdata.php )</p>
  
  <pre><code> &lt;?php
      /**
       *  PHP class for downloading CSV files from Google Webmaster Tools.
       *
       *  This class does NOT require the Zend gdata package be installed
       *  in order to run.
       *
       *  Copyright 2012 eyecatchUp UG. All Rights Reserved.
       *
       *  Licensed under the Apache License, Version 2.0 (the "License");
       *  you may not use this file except in compliance with the License.
       *  You may obtain a copy of the License at
       *
       *     http://www.apache.org/licenses/LICENSE-2.0
       *
       *  Unless required by applicable law or agreed to in writing, software
       *  distributed under the License is distributed on an "AS IS" BASIS,
       *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       *  See the License for the specific language governing permissions and
       *  limitations under the License.
       *
       *  @author: Stephan Schmitz &lt;eyecatchup@gmail.com&gt;
       *  @link:   https://code.google.com/p/php-webmaster-tools-downloads/
       */
  
       class GWTdata
       {
          const HOST = "https://www.google.com";
          const SERVICEURI = "/webmasters/tools/";
  
          public $_language, $_tables, $_daterange, $_downloaded, $_skipped;
          private $_auth, $_logged_in;
  
          public function __construct()
          {
              $this-&gt;_auth = false;
              $this-&gt;_logged_in = false;
              $this-&gt;_language = "en";
              $this-&gt;_daterange = array("","");
              $this-&gt;_tables = array("TOP_PAGES", "TOP_QUERIES",
                  "CRAWL_ERRORS", "CONTENT_ERRORS", "CONTENT_KEYWORDS",
                  "INTERNAL_LINKS", "EXTERNAL_LINKS", "SOCIAL_ACTIVITY"
              );
              $this-&gt;_errTablesSort = array(0 =&gt; "http",
                  1 =&gt; "not-found", 2 =&gt; "restricted-by-robotsTxt",
                  3 =&gt; "unreachable", 4 =&gt; "timeout", 5 =&gt; "not-followed",
                  "kAppErrorSoft-404s" =&gt; "soft404", "sitemap" =&gt; "in-sitemaps"
              );
              $this-&gt;_errTablesType = array(0 =&gt; "web-crawl-errors",
                  1 =&gt; "mobile-wml-xhtml-errors", 2 =&gt; "mobile-chtml-errors",
                  3 =&gt; "mobile-operator-errors", 4 =&gt; "news-crawl-errors"
              );
              $this-&gt;_downloaded = array();
              $this-&gt;_skipped = array();
          }
  
          /**
           *  Sets content language.
           *
           *  @param $str     String   Valid ISO 639-1 language code, supported by Google.
           */
              public function SetLanguage($str)
              {
                  $this-&gt;_language = $str;
              }
  
          /**
           *  Sets features that should be downloaded.
           *
           *  @param $arr     Array   Valid array values are:
           *                          "TOP_PAGES", "TOP_QUERIES", "CRAWL_ERRORS", "CONTENT_ERRORS",
           *                          "CONTENT_KEYWORDS", "INTERNAL_LINKS", "EXTERNAL_LINKS",
           *                          "SOCIAL_ACTIVITY".
           */
              public function SetTables($arr)
              {
                  if(is_array($arr) &amp;&amp; !empty($arr) &amp;&amp; sizeof($arr) &lt;= 2) {
                      $valid = array("TOP_PAGES","TOP_QUERIES","CRAWL_ERRORS","CONTENT_ERRORS",
                        "CONTENT_KEYWORDS","INTERNAL_LINKS","EXTERNAL_LINKS","SOCIAL_ACTIVITY");
                      $this-&gt;_tables = array();
                      for($i=0; $i &lt; sizeof($arr); $i++) {
                          if(in_array($arr[$i], $valid)) {
                              array_push($this-&gt;_tables, $arr[$i]);
                          } else { throw new Exception("Invalid argument given."); }
                      }
                  } else { throw new Exception("Invalid argument given."); }
              }
  
          /**
           *  Sets daterange for download data.
           *
           *  @param $arr     Array   Array containing two ISO 8601 formatted date strings.
           */
              public function SetDaterange($arr)
              {
                  if(is_array($arr) &amp;&amp; !empty($arr) &amp;&amp; sizeof($arr) == 2) {
                      if(self::IsISO8601($arr[0]) === true &amp;&amp;
                        self::IsISO8601($arr[1]) === true) {
                          $this-&gt;_daterange = array(str_replace("-", "", $arr[0]),
                            str_replace("-", "", $arr[1]));
                          return true;
                      } else { throw new Exception("Invalid argument given."); }
                  } else { throw new Exception("Invalid argument given."); }
              }
  
          /**
           *  Returns array of downloaded filenames.
           *
           *  @return  Array   Array of filenames that have been written to disk.
           */
              public function GetDownloadedFiles()
              {
                  return $this-&gt;_downloaded;
              }
  
          /**
           *  Returns array of downloaded filenames.
           *
           *  @return  Array   Array of filenames that have been written to disk.
           */
              public function GetSkippedFiles()
              {
                  return $this-&gt;_skipped;
              }
  
          /**
           *  Checks if client has logged into their Google account yet.
           *
           *  @return Boolean  Returns true if logged in, or false if not.
           */
              private function IsLoggedIn()
              {
                  return $this-&gt;_logged_in;
              }
  
          /**
           *  Attempts to log into the specified Google account.
           *
           *  @param $email  String   User's Google email address.
           *  @param $pwd    String   Password for Google account.
           *  @return Boolean  Returns true when Authentication was successful,
           *                   else false.
           */
              public function LogIn($email, $pwd)
              {
                  $url = self::HOST . "/accounts/ClientLogin";
                  $postRequest = array(
                      'accountType' =&gt; 'HOSTED_OR_GOOGLE',
                      'Email' =&gt; $email,
                      'Passwd' =&gt; $pwd,
                      'service' =&gt; "sitemaps",
                      'source' =&gt; "Google-WMTdownloadscript-0.1-php"
                  );
                  $ch = curl_init();
                  curl_setopt($ch, CURLOPT_URL, $url);
                  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
                  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                  curl_setopt($ch, CURLOPT_POST, true);
                  curl_setopt($ch, CURLOPT_POSTFIELDS, $postRequest);
                  $output = curl_exec($ch);
                  $info = curl_getinfo($ch);
                  curl_close($ch);
                  if($info['http_code'] == 200) {
                      preg_match('/Auth=(.*)/', $output, $match);
                      if(isset($match[1])) {
                          $this-&gt;_auth = $match[1];
                          $this-&gt;_logged_in = true;
                          return true;
                      } else { return false; }
                  } else { return false; }
              }
  
          /**
           *  Attempts authenticated GET Request.
           *
           *  @param $url    String   URL for the GET request.
           *  @return Mixed  Curl result as String,
           *                 or false (Boolean) when Authentication fails.
           */
              public function GetData($url)
              {
                  if(self::IsLoggedIn() === true) {
                      $url = self::HOST . $url;
                      $head = array("Authorization: GoogleLogin auth=".$this-&gt;_auth,
                          "GData-Version: 2");
                      $ch = curl_init();
                      curl_setopt($ch, CURLOPT_URL, $url);
                      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
                      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                      curl_setopt($ch, CURLOPT_ENCODING, true);
                      curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
                      $result = curl_exec($ch);
                      $info = curl_getinfo($ch);
                      curl_close($ch);
                      return ($info['http_code']!=200) ? false : $result;
                  } else { return false; }
              }
  
          /**
           *  Gets all available sites from Google Webmaster Tools account.
           *
           *  @return Mixed  Array with all site URLs registered in GWT account,
           *                 or false (Boolean) if request failed.
           */
              public function GetSites()
              {
                  if(self::IsLoggedIn() === true) {
                      $feed = self::GetData(self::SERVICEURI."feeds/sites/");
                      if($feed !== false) {
                          $sites = array();
                          $doc = new DOMDocument();
                          $doc-&gt;loadXML($feed);
                          foreach ($doc-&gt;getElementsByTagName('entry') as $node) {
                              array_push($sites,
                                $node-&gt;getElementsByTagName('title')-&gt;item(0)-&gt;nodeValue);
                          }
                          return $sites;
                      } else { return false; }
                  } else { return false; }
              }
  
          /**
           *  Gets the download links for an available site
           *  from the Google Webmaster Tools account.
           *
           *  @param $url    String   Site URL registered in GWT.
           *  @return Mixed  Array with keys TOP_PAGES and TOP_QUERIES,
           *                 or false (Boolean) when Authentication fails.
           */
              public function GetDownloadUrls($url)
              {
                  if(self::IsLoggedIn() === true) {
                      $_url = sprintf(self::SERVICEURI."downloads-list?hl=%s&amp;siteUrl=%s",
                        $this-&gt;_language,
                        urlencode($url));
                      $downloadList = self::GetData($_url);
                      return json_decode($downloadList, true);
                  } else { return false; }
              }
  
          /**
           *  Downloads the file based on the given URL.
           *
           *  @param $site    String   Site URL available in GWT Account.
           *  @param $savepath  String   Optional path to save CSV to (no trailing slash!).
           */
              public function DownloadCSV($site, $savepath=".")
              {
                  if(self::IsLoggedIn() === true) {
                      $downloadUrls = self::GetDownloadUrls($site);
                      $filename = parse_url($site, PHP_URL_HOST) ."-". date("Ymd-His");
                      $tables = $this-&gt;_tables;
                      foreach($tables as $table) {
                          if($table=="CRAWL_ERRORS") {
                              self::DownloadCSV_CrawlErrors($site, $savepath);
                          }
                          elseif($table=="CONTENT_ERRORS") {
                              self::DownloadCSV_XTRA($site, $savepath,
                                "html-suggestions", "\)", "CONTENT_ERRORS", "content-problems-dl");
                          }
                          elseif($table=="CONTENT_KEYWORDS") {
                              self::DownloadCSV_XTRA($site, $savepath,
                                "keywords", "\)", "CONTENT_KEYWORDS", "content-words-dl");
                          }
                          elseif($table=="INTERNAL_LINKS") {
                              self::DownloadCSV_XTRA($site, $savepath,
                                "internal-links", "\)", "INTERNAL_LINKS", "internal-links-dl");
                          }
                          elseif($table=="EXTERNAL_LINKS") {
                              self::DownloadCSV_XTRA($site, $savepath,
                                "external-links-domain", "\)", "EXTERNAL_LINKS", "external-links-domain-dl");
                          }
                          elseif($table=="SOCIAL_ACTIVITY") {
                              self::DownloadCSV_XTRA($site, $savepath,
                                "social-activity", "x26", "SOCIAL_ACTIVITY", "social-activity-dl");
                          }
                          else {
                              $finalName = "$savepath/$table-$filename.csv";
                              $finalUrl = $downloadUrls[$table] ."&amp;prop=ALL&amp;db=%s&amp;de=%s&amp;more=true";
                              $finalUrl = sprintf($finalUrl, $this-&gt;_daterange[0], $this-&gt;_daterange[1]);
                              self::SaveData($finalUrl,$finalName);
                          }
                      }
                  } else { return false; }
              }
  
          /**
           *  Downloads "unofficial" downloads based on the given URL.
           *
           *  @param $site    String   Site URL available in GWT Account.
           *  @param $savepath  String   Optional path to save CSV to (no trailing slash!).
           */
              public function DownloadCSV_XTRA($site, $savepath=".", $tokenUri, $tokenDelimiter, $filenamePrefix, $dlUri)
              {
                  if(self::IsLoggedIn() === true) {
                      $uri = self::SERVICEURI . $tokenUri . "?hl=%s&amp;siteUrl=%s";
                      $_uri = sprintf($uri, $this-&gt;_language, $site);
                      $token = self::GetToken($_uri, $tokenDelimiter);
                      $filename = parse_url($site, PHP_URL_HOST) ."-". date("Ymd-His");
                      $finalName = "$savepath/$filenamePrefix-$filename.csv";
                      $url = self::SERVICEURI . $dlUri . "?hl=%s&amp;siteUrl=%s&amp;security_token=%s&amp;prop=ALL&amp;db=%s&amp;de=%s&amp;more=true";
                      $_url = sprintf($url, $this-&gt;_language, $site, $token, $this-&gt;_daterange[0], $this-&gt;_daterange[1]);
                      self::SaveData($_url,$finalName);
                  } else { return false; }
              }
  
          /**
           *  Downloads the Crawl Errors file based on the given URL.
           *
           *  @param $site    String   Site URL available in GWT Account.
           *  @param $savepath  String   Optional: Path to save CSV to (no trailing slash!).
           *  @param $separated Boolean  Optional: If true, the method saves separated CSV files
           *                             for each error type. Default: Merge errors in one file.
           */
              public function DownloadCSV_CrawlErrors($site, $savepath=".", $separated=false)
              {
                  if(self::IsLoggedIn() === true) {
                      $type_param = "we";
                      $filename = parse_url($site, PHP_URL_HOST) ."-". date("Ymd-His");
                      if($separated) {
                          foreach($this-&gt;_errTablesSort as $sortid =&gt; $sortname) {
                              foreach($this-&gt;_errTablesType as $typeid =&gt; $typename) {
                                  if($typeid == 1) {
                                      $type_param = "mx";
                                  } else if($typeid == 2) {
                                      $type_param = "mc";
                                  } else {
                                      $type_param = "we";
                                  }
                                  $uri = self::SERVICEURI."crawl-errors?hl=en&amp;siteUrl=$site&amp;tid=$type_param";
                                  $token = self::GetToken($uri,"x26");
                                  $finalName = "$savepath/CRAWL_ERRORS-$typename-$sortname-$filename.csv";
                                  $url = self::SERVICEURI."crawl-errors-dl?hl=%s&amp;siteUrl=%s&amp;security_token=%s&amp;type=%s&amp;sort=%s";
                                  $_url = sprintf($url, $this-&gt;_language, $site, $token, $typeid, $sortid);
                                  self::SaveData($_url,$finalName);
                              }
                          }
                      }
                      else {
                          $uri = self::SERVICEURI."crawl-errors?hl=en&amp;siteUrl=$site&amp;tid=$type_param";
                          $token = self::GetToken($uri,"x26");
                          $finalName = "$savepath/CRAWL_ERRORS-$filename.csv";
                          $url = self::SERVICEURI."crawl-errors-dl?hl=%s&amp;siteUrl=%s&amp;security_token=%s&amp;type=0";
                          $_url = sprintf($url, $this-&gt;_language, $site, $token);
                          self::SaveData($_url,$finalName);
                      }
                  } else { return false; }
              }
  
          /**
           *  Saves data to a CSV file based on the given URL.
           *
           *  @param $finalUrl   String   CSV Download URI.
           *  @param $finalName  String   Filepointer to save location.
           */
              private function SaveData($finalUrl, $finalName)
              {
                  $data = self::GetData($finalUrl);
                  if(strlen($data) &gt; 1 &amp;&amp; file_put_contents($finalName, utf8_decode($data))) {
                      array_push($this-&gt;_downloaded, realpath($finalName));
                      return true;
                  } else {
                      array_push($this-&gt;_skipped, $finalName);
                      return false;
                  }
              }
  
          /**
           *  Regular Expression to find the Security Token for a download file.
           *
           *  @param $uri        String   A Webmaster Tools Desktop Service URI.
           *  @param $delimiter  String   Trailing delimiter for the regex.
           *  @return  String    Returns a security token.
           */
              private function GetToken($uri, $delimiter)
              {
                  $matches = array();
                  $tmp = self::GetData($uri);
                  //preg_match_all("#x26security_token(.*?)$delimiter#si", $tmp, $matches);
                  preg_match_all("#46security_token(.*?)$delimiter#si", $tmp, $matches); 
                  //return substr($matches[1][0],4,-1);
                  return substr($matches[1][0],3,-1);
              }
  
          /**
           *  Validates ISO 8601 date format.
           *
           *  @param $str      String   Valid ISO 8601 date string (eg. 2012-01-01).
           *  @return  Boolean   Returns true if string has valid format, else false.
           */
              private function IsISO8601($str)
              {
                  $stamp = strtotime($str);
                  return (is_numeric($stamp) &amp;&amp; checkdate(date('m', $stamp),
                        date('d', $stamp), date('Y', $stamp))) ? true : false;
              }
       }
  ?&gt;
  </code></pre>
  
  <p><em><strong>File #2:</em></strong> ( index.php )</p>
  
  <pre><code>&lt;?php
  include 'gwtdata.php';
  include 'credentials.php';
  try {  
        $website = "http://www.yourdomain.com/"; /* Add Your Website Url */             
        $gdata = new GWTdata();
        if($gdata-&gt;LogIn($email, $password) === true) 
        {                             
        $gdata-&gt;DownloadCSV($website,"Here Add Your Folder Path To Save CSV File With GWT Data");                     
        echo "Datas Are Successfully Downloaded";
        }
      } catch (Exception $e) {
           die($e-&gt;getMessage());
        }
  ?&gt;
  </code></pre>
  
  <p>Can anyone help me in this, to achieve all those datas and make it as excel file to generate using PHP.</p>