How to create multiple XML sitemap in PHP

multiple XML sitemap Get Webpage Title Textbox Dynamically crawl password protecteed PHP Interview Questions

I am sharing How to create multiple XML sitemap in PHP. You can submit 50,000 URL’s per XML sitemap in google.If you are use I have created method the you can submit URL in Google.And you want to submit all URL’s to google then this method will help you to create dynamic XML sitemap.

Also Read: How to integrate SMS API in PHP

Here is Method for create multiple XML sitemap in PHP

allsitemap.php

<?php
  header('Content-type: application/xml');
  $hostname = "localhost";
$username = "username";
$password = "password";
$dbname = "booksdb";
$con = mysqli_connect($hostname, $username, $password, $dbname);
 $query = "SELECT name, author, id FROM books WHERE  status=1";
    $result = mysqli_query($con, $query);
$count = mysqli_num_rows($result);
$output = "<?xml version='1.0' encoding='UTF-8'?>";
  $output .= "<sitemapindex xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>";
    for($i=0;$i<=$count;$i+=25000) {
  $output .= "<sitemap>";
  $output .=    "<loc>http://example.com/books/sitemap.php?limit=".$i."</loc>"; // location of sitemaps
  $output .= "</sitemap>";
  }
  echo $output .= "</sitemapindex>";
  exit;

Above code will create multiple sitemaps with 25,000 url’s each. Suppose you have 10 lakh books in database then it’ll create 40 sitemap like this.
OUTPUT:
http://example.com/books/sitemap.php?limit = 0;
http://example.com/books/sitemap.php?limit = 25000;
http://example.com/books/sitemap.php?limit = 50000;
http://example.com/books/sitemap.php?limit = 100000;
.. . . . . . . . . . . . . . .
… . . . . . . . . . . . . . .. . and so on
http://example.com/books/sitemap.php?limit = 900000;
http://example.com/books/sitemap.php?limit = 950000;

 

sitemap.php

<?php
  header('Content-type: application/xml');
  $baseurl = "http://example.com/books/";
  $hostname = "localhost";
$username = "username";
$password = "password";
$dbname = "booksdb";
$limit = $_GET['limit'];
$con = mysqli_connect($hostname, $username, $password, $dbname);
 
 function clean($string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
 
   return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}
 
  $output = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
  $output .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
  echo $output;
?>
  <?php $query = "SELECT name, author, id FROM books WHERE  status=1 LIMIT 0, $limit";
    $result = mysqli_query($con, $query);
    $res = array();
 
    while($resultSet = mysqli_fetch_assoc($result)) { 
 
 if(!empty($resultSet['name'])) { ?>
 
<url>
  <loc><?php echo $baseurl.clean(trim($resultSet['name'])).'/'. clean(trim($resultSet['author'])).'/'.$resultSet['id']; ?></loc>
</url>
<?php }  } ?>
</urlset>

All done now you can submit allsitemap.php single sitemap file in google web master which automatically submit all books urls in google web master, you don’t need to create more sitemaps. Only this single file generate multiple sitemaps with 25000 URL’s each.

Also Read: How to get Webpage Title and Meta Descriptin from URL in PHP

Leave a Reply