×
BLUE
How to create Dynamic sitemap.xml in php


How to create Dynamic sitemap.xml in php
  11 August 2021
Read more Like this Post.
Lets create a sitemap for website in php. this sitemap is generate php automatically and fetch all posts links from database table and write a sitemap in every call.
This is a simple php file which generate posts all links and write in you sitemap.xml file. we can call this file in sometime or when we upload a new post in database this will rewrite the xml file for us.
let's jump into the code.
sitemap_generator.php
$con = mysqli_connect("localhost", "root", "", "myDataBaseName");
$blogs = $con->query("SELECT `url` FROM `blogs` WHERE 1");
connection to database and fetch all post links in blogs. here some people use slug or id as post urls.
$blogs_data = array();
this is a array to collect all post urls here.
if ($blogs->num_rows > 0) {
    while ($row = $blogs->fetch_assoc()) {
        $blogs_data[] = $row;
    }
}
this while fetching data and add into $blog_data array.
constant urls of website.
$static_values = [
    "https://www.jozeboy.com/",
    "https://www.jozeboy.com/blogs/",
    "https://www.jozeboy.com/contact/",
    "https://www.jozeboy.com/abouts/",
];

Here is some constant urls like root url like https://www.jozeboy.com or like https://www.jozeboy.com/blogs etc this urls is not coming from database. therefore we write this in a static array.
$sitemap_text = 'the $sitemap_text variable store the xml data there we start with xml urlset tag.
foreach ($static_values as $value) {
    $mod_date = date('c');
    $priority = '0.80';
    if ($value == 'https://www.jozeboy.com/') $priority = '1.00';
    $sitemap_text .=
      "
        $value
            $mod_date
            $priority
            weekly
        ";
}

this foreach iterate in $static_values and add this in our $sitemap_text variable. for adding strings to strings we use ( . ) operator. there is some other variable like $mod_date this var use for date which use in xml lastmod tag. and seconds is priority we set default priority 0.80 and set this 1 after if condition. if $value of $static_values array is equal to root url set priority 1.00 other than its 0.80. now we add static urls now lets add blogs post urls in it.
$blogs_data array.

foreach ($blogs_data as $url) {
    $mod_date = date('c');
    $priority = '0.64';
    $url = $url['url'];
    $sitemap_text .=
         "
            https://www.jozeboy.com/blue/$url
            $mod_date
            $priority
        ";
}

the same way of adding $static_values is applies here. only we remove if condition. change the priority to 0.64 if you want you can change it as you want.
lets close the opening tags of xml file
$sitemap_text .= '';
we close the open tag urlset of xml.
now lets write the $sitemap_text strings into sitemap.xml file.
$sitemap = fopen('sitemap.xml', 'w');
fwrite($sitemap, $sitemap_text);
fopen() function open the which is in same directory. and second parameters is open the file as writeable and we write the file for simply call fwrite() function which take two parameters one of the target file and seconds is the text which we want to write on it. after finishing all code let try on it call this php file on your server and look the resultant file.
finally we write the xml sitemap file for website now keep this sitemap file in server root directory and keep php file where you want to call this file easily. Thanks

Related Post's

sitemap   SEO   PHP   XML  
Not Loaded Zohaib Jozvi
0 11 August 2021
Read Latest Post.
Read other post.