Google Webmaster tool api with Zend php, framework 1.7.
Google webmaster API php - php - example.
I did a project for my company 4 months ago so its a little bit hard to remember everything.
Let's start!
How can you add 1000 websites in you google webmaster tool?
You can't do it manually, you don't have enough time...
You have to add your siteweb, verify it and after you can push the sitemap.
Here, my code:
Firstly :
create a form with login and password
Init :
$service = 'sitemaps';
global $source ;
$source = 'Zend_ZendFramework-0.1.1';
//framework zend
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Feed');
Login to Google :
$logintoken = isset( $_POST['captchatoken']) ? $_POST['captchatoken'] : '';
$resp = tryconnect($_POST['username'],$_POST['passwd'],$logintoken,$logincaptcha );
function tryconnect($username,$password,$logintoken ){
$service = 'sitemaps';
$client = Zend_Gdata_ClientLogin::getHttpClient($username, $password, $service);
return $client;
}
Add a site :
global $service;
$client = Zend_Gdata_ClientLogin::getHttpClient($resp['username'],$resp['passwd'], $service);
$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);
}
$xml='';
$xml.='';
$xml.='';
$fdata = new Zend_Gdata($client);
try{
$result = $fdata->post($xml,"https://www.google.com/webmasters/tools/feeds/sites/");
if($result != ''){
echo "
Domain: ".$domain." has been submitted to Google Webmaster";
return 1;
}elseif( preg_match('/Duplicate url/i',$result)){
echo "
Domain: ".$domain." is already in Google Webmaster";
}else{
echo "
Domain: ".$domain." : failed!";
return 0;
}
}catch (Exception $e){
if (preg_match('/Duplicate url/i',$e->getMessage()) ){
echo "
Domain: ".$domain." is already in Google Webmaster";
}else{
echo "
Domain: ".$domain." error: ".$e->getMessage()."";
}
}
}
GET THE FEED :
if ($resp->getClientLoginToken()){
$client = Zend_Gdata_ClientLogin::getHttpClient($resp['username'],$resp['passwd'], $service);
$gdata = new Zend_Gdata($client);
$feed = $gdata->getFeed("https://www.google.com/webmasters/tools/feeds/sites/");
}
Get the feed url
function listFeedUrl($resp){//not currently used
global $service;
$client = Zend_Gdata_ClientLogin::getHttpClient($resp['username'],$resp['passwd'],$service);
$gdata = new Zend_Gdata($client);
$feed = $gdata->getFeed("https://www.google.com/webmasters/tools/feeds/sites/");
foreach ($feed as $item) {
echo '
' . $item->title() . '
';}
}
Increase the set time out limit
you are limited to 1000 websites per G Account.
Verify website:
$client = Zend_Gdata_ClientLogin::getHttpClient($resp['username'],$resp['passwd'], $service);
$gdata = new Zend_Gdata($client);
$feed = $gdata->getFeed("https://www.google.com/webmasters/tools/feeds/sites/");
$count=0;
$feed=str_replace('wt:','',$feed); //not nice I found the solution somewhere I have to update my code here
$feed=str_replace('/wt:','',$feed);
$feed=str_replace('-method','',$feed);
$feed=str_replace('<','',$feed);
$feed=simplexml_load_string($feed);
$nbentry=count($feed->entry);
for ($i=0;$i<$nbentry;$i++) {
if($feed->entry[$i]->verified=='false'){
$subjects = $feed->entry[$i]->verification[0];
list($meta,$name,$content)=split(' ',$subjects);
$content;
$content=str_replace('content="','',$content);
$key=str_replace('"','',$content);
$count++;
$xml='
xmlns:wt="http://schemas.google.com/webmasters/tools/2007">
'.$feed->entry[$i]->title.'
term="http://schemas.google.com/webmasters/tools/2007#site-info"/>
';
$domain=urlencode($feed->entry[$i]->title);
$domain=str_replace('.',"%2E",$domain);
//$fdata = new Zend_Gdata($client);
$result=$gdata->put($xml,"https://www.google.com/webmasters/tools/feeds/sites/".$domain);
//$result->getStatus();
//print_r($result);
if($result->getStatus()=='200'){
echo "
".$count.' '.$feed->entry[$i]->title." : ok.";
}else{
echo "
".$count.' '.$feed->entry[$i]->title." : failed!";
}
}
}echo "
total".$count;
Add sitemap to G
$gdata = new Zend_Gdata($client);
$feed = $gdata->getFeed("https://www.google.com/webmasters/tools/feeds/sites/");
$i=0;
foreach ($feed as $item) {
if($item->verified()=='true'){
$i++;
$ret = addWebmap($item->title(), $item->title().'sitemap.php', $resp);
}
}
echo "
nb: ".$i;
Remove a sitemap:
global $service;
$client = Zend_Gdata_ClientLogin::getHttpClient($resp['username'],$resp['passwd'], $service);
$fdata = new Zend_Gdata($client);
$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=$fdata->delete($myaddress);
}
Get message
navigation();
global $service;
$client = Zend_Gdata_ClientLogin::getHttpClient($resp['username'],$resp['passwd'], $service);
$gdata = new Zend_Gdata($client);
$feed = $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++;$ientry)) {
echo $feed->entry[$i]->title();
echo $feed->entry[$i]->body();//not sure about this but no messages to test...
$ctmessages++;
}
if($ctmessages==0)
echo "
No messages";
}
My code is based on Juust's work, you can find more here : www.juust.org .
I hope it helps someone
My file is here.
SanoZ