Google Sitemap Generator with PHP

This is the PHP code that runs the Blogger Sitemap Generator. The generated XML sitemaps are exposed to Google via the robots.txt file - see Add Google Sitemap to Blogger Blogs.

// Make sure that input contains http
 if(substr($blog,0,7) != "http://")
  $blog = "http://" . $blog;

// Make sure that blog URL ends with a slash
 if(substr($blog, -1) != "/")
  $blog = $blog . "/";

// Determine the count of blog posts
 $url = $blog . "atom.xml?redirect=false&start-index=1&max-results=1";

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

 $xml = curl_exec($ch) ;
 curl_close($ch);

// Parse the XML ouput to determine the count
 $x = strpos($xml, "<openSearch:totalResults>") + strlen("<openSearch:totalResults>");
 $y = strpos($xml, "</openSearch:totalResults>");
 $c = substr($xml, $x, $y-$x);

// Generate the XML sitemap for robots.txt
  if ($c >=1) {
    echo "# Blogger Sitemap generated on " . date("Y.m.d") . "<br />";
    echo "User-agent: *<br/>Disallow: /search<br/>Allow: /<br />";
    for($x=1; $x<=$c; $x=$x+500) {
        echo "<br/>Sitemap: " . $blog . "atom.xml?redirect=false&start-index=" . $x . "&max-results=500";
     }
  }
Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

0

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.