Search code examples
phpxmlhttp-headersgeneratorsitemap

Sitemap Dynamic Creation with PHP


I'm trying to generate valid sitemap with php. The logic is simple. I forwarded all ^(.+)index_sitemal.xml request to my index_sitemap.php file in .htaccess. PHP script is below:

<?php 
header( "content-type: application/xml; charset=UTF-8" );
    echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
    echo '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n";
    $len = 10;   // to take
    $min = 50;  // minimum
    $max = 100;  // maximum
    $range = [];
    foreach (range(0, $len - 1) as $i) {
        while(in_array($num = mt_rand($min, $max), $range));
        $range[] = $num;
        echo '<sitemap><loc>http://'.$_SERVER['SERVER_NAME'].'/sitemap/'.$num.'.xml</loc></sitemap>'."\n";
    }
    echo '</sitemapindex>';
?>

In browser it displays good.

Link for image - (sorry low reputation) https://i.ibb.co/4ZmLJ1D/Screenshot-at-Jan-29-10-47-01.png

But while trying to validate the xml I'm getting type error.

Link for image - (sorry low reputation) https://i.ibb.co/Ws11cBj/Screenshot-at-Jan-29-10-55-28.png

Is there any way to display a dynamic sitemap using php?


Solution

  • I had a similar issue before but solved it by running a different PHP file to update the sitemap with a CRON job.

    <?php
    $xmlString = '<?xml version="1.0" encoding="UTF-8"?>';
    $xmlString .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
    
    $xmlString .= '<url>';
    $xmlString .= '<loc>http://example.com/</loc>';
    $xmlString .= '<lastmod>'.date(DATE_ATOM,time()).'</lastmod>';
    $xmlString .= '<changefreq>daily</changefreq>';
    $xmlString .= '<priority>1.0</priority>';
    $xmlString .= '</url>';
    
    $xmlString .= '<url>';
    $xmlString .= '<loc>http://example.com/videos/</loc>';
    $xmlString .= '<lastmod>'.date(DATE_ATOM,time()).'</lastmod>';
    $xmlString .= '<changefreq>daily</changefreq>';
    $xmlString .= '<priority>1.0</priority>';
    $xmlString .= '</url>';
    
    $xmlString .= '<url>';
    $xmlString .= '<loc>http://example.com/contact/</loc>';
    $xmlString .= '<lastmod>'.date(DATE_ATOM,time()).'</lastmod>';
    $xmlString .= '<changefreq>daily</changefreq>';
    $xmlString .= '<priority>1.0</priority>';
    $xmlString .= '</url>';
    
    $xmlString .= '<url>';
    $xmlString .= '<loc>http://example.com/blog/</loc>';
    $xmlString .= '<lastmod>'.date(DATE_ATOM,time()).'</lastmod>';
    $xmlString .= '<changefreq>daily</changefreq>';
    $xmlString .= '<priority>1.0</priority>';
    $xmlString .= '</url>';
    
    $sql = "SELECT * FROM categories";
    $stmt = DB::run($sql);
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        $url = $row["url"];
        $xmlString .= '<url>';
        $xmlString .= '<loc>http://example.com/category/'.htmlentities($url).'/</loc>';
        $xmlString .= '<lastmod>'.date(DATE_ATOM,time()).'</lastmod>';
        $xmlString .= '<changefreq>daily</changefreq>';
        $xmlString .= '<priority>1.0</priority>';
        $xmlString .= '</url>';
    }
    
    $xmlString .= '</urlset>';
    
    $dom = new DOMDocument;
    $dom->preserveWhiteSpace = FALSE;
    $dom->loadXML($xmlString);
    
    $dom->save('../sitemap.xml');
    ?>
    

    Edit

    <?php
    
    $host = 'http://www.google.com/ping?sitemap=https://www.example.com/sitemap.xml'; 
    $ch = curl_init($host);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if($httpcode>=200 && $httpcode<300){
      echo 'success';
    } else {
      echo 'try again';
    }
    
    ?>