How to Submit Sitemaps using PHP automatically?


Sitemaps are useful for search bots to know better your site structures. They are particular useful if your site contains several hundred pages. You can login to Google Webmaster or Bing Master (or other similar website tools) to submit the sitemaps manually. If you have many sitemaps (or many websites), this will be a headache. How about submitting sitemaps automatically (to let search engines know you have updates)?

sitemap How to Submit Sitemaps using PHP automatically? crontab php sitemaps

sitemap

First define the sitemaps URL array using PHP:

1
2
3
4
5
6
7
8
9
10
$sitemaps = array(
  "https://helloacm.com/sitemap.xml",
  "https://codingforspeed.com/sitemap.xml",
  "https://codingforspeed.com/forum/sitemap.php",
  "https://uploadbeta.com/sitemap.xml",
  "https://rot47.net/sitemap.xml",
  "https://justyy.com/sitemap.xml",
  "https://steakovercooked.com/sitemap.xml",
  "https://steakovercooked.com/wedding/sitemap.xml"
);
$sitemaps = array(
  "https://helloacm.com/sitemap.xml",
  "https://codingforspeed.com/sitemap.xml",
  "https://codingforspeed.com/forum/sitemap.php",
  "https://uploadbeta.com/sitemap.xml",
  "https://rot47.net/sitemap.xml",
  "https://justyy.com/sitemap.xml",
  "https://steakovercooked.com/sitemap.xml",
  "https://steakovercooked.com/wedding/sitemap.xml"
);

Then define two functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// cUrl handler to ping the Sitemap submission URLs for Search Engines…
function Submit($url){
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_exec($ch);
  $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_close($ch);
  return $httpCode;
}
 
function SubmitSiteMap($url) {
  $returnCode = Submit($url);
  if ($returnCode != 200) {
    echo "Error $returnCode: $url <BR/>";
  } else {
    echo "Submitted $returnCode: $url <BR/>";
  }
}
// cUrl handler to ping the Sitemap submission URLs for Search Engines…
function Submit($url){
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_exec($ch);
  $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_close($ch);
  return $httpCode;
}

function SubmitSiteMap($url) {
  $returnCode = Submit($url);
  if ($returnCode != 200) {
    echo "Error $returnCode: $url <BR/>";
  } else {
    echo "Submitted $returnCode: $url <BR/>";
  }
}

Then all you need to do is to iterative all sitemaps and submit them to the different search engines:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
foreach ($sitemaps as $sitemapUrl) {
  $sitemapUrl = htmlentities($sitemapUrl);
 
  //Google  
  $url = "http://www.google.com/webmasters/sitemaps/ping?sitemap=".$sitemapUrl;
  SubmitSiteMap($url);
  
  //Bing / MSN
  $url = "http://www.bing.com/webmaster/ping.aspx?siteMap=".$sitemapUrl;
  SubmitSiteMap($url);
  
  // Live
  $url = "http://webmaster.live.com/ping.aspx?siteMap=".$sitemapUrl;
  SubmitSiteMap($url);
  
  // moreover
  $url = "http://api.moreover.com/ping?sitemap=".$sitemapUrl;
  SubmitSiteMap($url);
}
foreach ($sitemaps as $sitemapUrl) {
  $sitemapUrl = htmlentities($sitemapUrl);

  //Google  
  $url = "http://www.google.com/webmasters/sitemaps/ping?sitemap=".$sitemapUrl;
  SubmitSiteMap($url);
  
  //Bing / MSN
  $url = "http://www.bing.com/webmaster/ping.aspx?siteMap=".$sitemapUrl;
  SubmitSiteMap($url);
  
  // Live
  $url = "http://webmaster.live.com/ping.aspx?siteMap=".$sitemapUrl;
  SubmitSiteMap($url);
  
  // moreover
  $url = "http://api.moreover.com/ping?sitemap=".$sitemapUrl;
  SubmitSiteMap($url);
}

Add the following to the beginning, which does not set a time limit, if you want to add this job in the crontab.

1
set_time_limit(999999);
set_time_limit(999999);

Finally, you can add this to crontab -e

1
@daily php submit_sitemap.php
@daily php submit_sitemap.php

And, the function Submit can be simplified by using file_get_contents if you don’t want to know if the submission is successful (no return code).

1
2
3
function Submit($url) {
  file_get_contents($url);
}
function Submit($url) {
  file_get_contents($url);
}

The full source code is available at github: submit_sitemaps.php

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
533 words
Last Post: How to retrieve Hard Drive Serial Number using Scripts (PowerShell + VBScript + Javascript) ?
Next Post: How to Compress Images using ImageRecycle API (The PHP Script)?

The Permanent URL is: How to Submit Sitemaps using PHP automatically?

Leave a Reply