Google Webmaster tool api with Zend php, framework 1.7 Version 2
Hi guys,
Google Webmaster Tool api in PHP
I changed my old code by a class.
The code is (almost) cleaned. I hope it helps you
Here the class code :
<?php
Class GoogleWebmaster{
public $connection ;
public $client;
public $gdata;
public $gdataApps;
public $feed = array();
private $service = 'sitemaps';
private $username;
private $password;
private $clientLogin;
public function __construct(){
require(dirname(__FILE__).'/../Zend/Gdata/ClientLogin.php');
}
public function _setService($value){
$this->service = $value;
}
/*
* connection to google
*/
public function connection($username,$password,$logintoken,$logincaptcha){
$this->username = $username;
$this->password = $password;
$this->getHttpClient();
$this->clientLogin = $this->client->getClientLoginToken();
}
private function getHttpClient(){
echo $this->service ;
$this->client = Zend_Gdata_ClientLogin::getHttpClient( $this->username, $this->password, $this->service );
}
public function Gdata (){
require(dirname(__FILE__).'/../Zend/Gdata.php');
$this->gdata = new Zend_Gdata($this->client);
}
private function getEntry($domain){
$domain = str_replace('/','%2F', $domain);
$domain = str_replace('.','%2E', $domain);
$domain = 'http%3A%2F%2F' .$domain."%2F";
return $this->feed = $this->gdata->getEntry("https://www.google.com/webmasters/tools/feeds/sites/". $domain );
}
private function getFeed($domain){
$domain = str_replace('/','%2F', $domain);
$domain = str_replace('.','%2E', $domain);
$domain = 'http%3A%2F%2F' .$domain."%2F";
return $this->feed = $this->gdata->getFeed("https://www.google.com/webmasters/tools/feeds/sites/". $domain );
}
public function getFeedSitemap($domain){
return $this->getFeed($domain );
}
/*
*
function addSiteMapToGoogle( ){
set_time_limit(0);
$i = 0;
foreach ($this->feed as $item) {
if($item->verified() == 'true'){
$i++;
$ret = $this->addWebmap($item->title(), $item->title().'sitemap.php' );
}
}
echo "<br>nb: ".$i;
}
*/
public function createUser($domain,$login, $username, $password, $familyname, $givenname ){
$xml="<?xml version='1.0' encoding='UTF-8'?>
<atom:entry xmlns:atom='http://www.w3.org/2005/Atom'
xmlns:apps='http://schemas.google.com/apps/2006'>
<apps:login userName='".$username."'
password='".$password."' />
<apps:quota limit='2048'/>
<apps:name familyName='".$familyname."' givenName='".$givenname."'/>
</atom:entry>";
$domain = $this->formatDomain( $domain);
$myaddress = "https://apps-apis.google.com/a/feeds/".$domain."/user/2.0";
$result = $this->gdata->post($xml,$myaddress);
echo 'code: ',$code = $result->getStatus() ;
//to check
if( $code == '201' ){
echo "<br/><span class='texte'> Domain: ".$domain." : ok.</span>";
}else{
echo "<br/><span class='texte'> Domain: ".$domain." : failed!</span>";
}
}
public function addWebmap($domain, $urlsitemap ) {
$xml="<atom:entry xmlns:atom='http://www.w3.org/2005/Atom'
xmlns:wt='http://schemas.google.com/webmasters/tools/2007'>" .
"<atom:id>".$urlsitemap."</atom:id>
<atom:category scheme='http://schemas.google.com/g/2005#kind'
term='http://schemas.google.com/webmasters/tools/2007#sitemap-regular'/>
<wt:sitemap-type>WEB</wt:sitemap-type>
</atom:entry>";
$domain = $this->formatDomain( $domain);
$myaddress = "https://www.google.com/webmasters/tools/feeds/".$domain."/sitemaps/";
$result = $this->gdata->post($xml,$myaddress);
$code = $result->getStatus() ;
//to check
if( $code == '201' ){
echo "<br/><span class='texte'> Domain: ".$domain." : ok.</span>";
}else{
echo "<br/><span class='texte'> Domain: ".$domain." : failed!</span>";
}
}
/* GWGW
* delete a sitemap to google
*/
public function delete_sitemap($domain,$resp){
$domainadr = urlencode($domain);
$domainadr = str_replace('.',"%2E",$domainadr);
$sitemapAdr = $domain."sitemap.phpsitemap.php";
$sitemapAdr = urlencode($sitemapAdr);
$sitemapAdr = str_replace('.',"%2E",$sitemapAdr);
$myaddress = "https://www.google.com/webmasters/tools/feeds/".$domainadr."/sitemaps/".$sitemapAdr;
$result = $this->gdata->delete($myaddress);
}
function verifySite($title, $key ) {
$domain = urlencode($title);
$domain = $this->formatDomain( $title);
$xml = '<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:wt="http://schemas.google.com/webmasters/tools/2007">
<atom:id>'.$domain.'</atom:id>
<atom:category scheme="http://schemas.google.com/g/2005#kind"
term="http://schemas.google.com/webmasters/tools/2007#site-info"/>
<wt:verification-method type="metatag" in-use="true"/>
</atom:entry>';
$result = $this->gdata->put($xml,"https://www.google.com/webmasters/tools/feeds/sites/".$domain);
if($result->getStatus()=='200'){
echo "<br/><span class='texte'>".$domain." : ok.</span>";
}else{
echo "<br/><span class='texte'>".$domain." : failed!</span>";
}
}
/* GWGW
* returns google message
*/
public function getMessages($resp){
$feed = $this->gdata->getFeed("https://www.google.com/webmasters/tools/feeds/messages/");
$feed = str_replace('wt:','',$feed);
$feed = str_replace('/wt:','',$feed);
$feed = str_replace('-method','',$feed);
$feed = str_replace('<','',$feed);
$feed = simplexml_load_string($feed);
$ctmessages=0;
for ($i=0;$i++;$i<count($feed->entry)) {
echo $feed->entry[$i]->title();
echo $feed->entry[$i]->body();//not sure about this but no messages to test...
$ctmessages++;
}
if($ctmessages==0)
echo "<br>No messages";
}
/*
* Maybe it will not work => change it in V2
* if you want to delete website
*/
public function delete_site($siteid ){
$siteid = trim($siteid);
echo '<br />',$siteid = strtolower($siteid);
$result = $this->gdata->delete("https://www.google.com/webmasters/tools/feeds/sites/".urlencode($siteid));
return 0;//$result;
}
public function add_site( $domain) {
set_time_limit(0);
$domain = trim($domain);
$domain = strtolower($domain);
//if you forgot to remove the http I will
if ( preg_match('~http://~',$domain)){
$domain = str_replace('http://','',$domain);
}
echo '<br />',$domain;
$xml='<entry xmlns="http://www.w3.org/2005/Atom">';
$xml.='<content src="http://'.$domain.'/" />';
$xml.='</entry>';
// $fdata = new Zend_Gdata($client);
try{
$result = $this->gdata->post($xml,"https://www.google.com/webmasters/tools/feeds/sites/");
if($result != ''){
echo "<br/><span class='texte'>Domain: ".$domain." has been submitted to Google Webmaster</span>";
return 1;
}elseif( preg_match('/Duplicate url/i',$result)){
echo "<br/><span class='texte'>Domain: ".$domain." is already in Google Webmaster</span>";
}else{
echo "<br/><span class='texte'>Domain: ".$domain." : failed!</span>";
return 0;
}
}catch (Exception $e){
if (preg_match('/Duplicate url/i',$e->getMessage()) ){
echo "<br/><span class='texte'>Domain: ".$domain." is already in Google Webmaster</span>";
}else{
echo "<br/><span class='texte'>Domain: ".$domain." error: ".$e->getMessage()."</span>";
}
}
}
public function cleaningFeed($feed){
$feed = str_replace("wt:" ,"",$feed);
$feed = str_replace('/wt:','',$feed);
$feed = str_replace("gd:" ,"",$feed);
$feed = str_replace("/gd:","",$feed);
$feed = str_replace('-method','',$feed);
// $feed = str_replace('<' ,'<',$feed);
// $feed = str_replace('/>' ,'>',$feed);
return $feed;
}
public function returnKey($verification){
try{
foreach($verification as $key => $values){
if (!preg_match('/\.html/i', $values)){
$feed = simplexml_load_string($values);
return isset($feed->attributes()->content ) ? $feed->attributes()->content : '';
}else{
echo "<br> found:".$values;
}
}
}catch (Exception $e){
throw new Exception('google webmaster class returns an error:'.$e->getMessage());
}
}
public function formatDomain($domain){
if(!preg_match('/http/i', $domain)){
$domain = 'http%3A%2F%2F' .$domain;
}
$domain = str_replace('/','%2F', $domain);
$domain = str_replace('.','%2E', $domain);
//for the end of the string
if ( substr($domain, -1) != "/" ){
$domain = $domain . "%2F";
}
return $domain ;
}
public function GdataService($domain){
require(dirname(__FILE__).'/../Zend/Gdata/Gapps.php');
$this->gdataApps = new Zend_Gdata_Gapps($this->client, $domain);
}
}
I hope it helps.
Sanoz