Using a dynamic sitemap

Hi everyone,

I have read for a while about this, but I have some problems with the sitemap.php generated with PHP and the process I have to follow.

The code I use to create the sitemap is the following:

<?php

header('Content-type: application/xml');
echo "<?xml version='1.0' encoding='UTF-8'?>";
echo "<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'
        xmlns:news='http://www.google.com/schemas/sitemap-news/0.9'>";

    require 'admin/config.php';
    require 'funciones.php';

    try {
        $conexion = new PDO($bd_config['dbname'], $bd_config['usuario'], $bd_config['password'] );
    } catch (PDOException $e) {
        header ('Location: error.php');
        echo "ERROR: ".$e->getMessage();
        die();
    }



    $hoy = getdate();

    $fecha_hoy = $hoy['year'] . "-" . $hoy['mon'] . "-" . $hoy['mday'];

    echo "<url>";
      echo "<loc>http://www.laxtore.com/</loc>";
      echo "<lastmod>" . $fecha_hoy . "</lastmod>";
      echo "<changefreq>daily</changefreq>";
      echo "<priority>1</priority>";
    echo "</url>";

    echo "<url>";
      echo "<loc>http://www.laxtore.com/Juegos/Indice-Precios/</loc>";
      echo "<lastmod>" . $fecha_hoy . "</lastmod>";
      echo "<changefreq>daily</changefreq>";
      echo "<priority>1</priority>";
    echo "</url>";

    echo "<url>";
      echo "<loc>http://www.laxtore.com/contacto/</loc>";
      echo "<priority>0.8</priority>";
    echo "</url>";

    echo "<url>";
      echo "<loc>http://www.laxtore.com/Juegos/Xbox-One/</loc>";
      echo "<lastmod>" . $fecha_hoy . "</lastmod>";
      echo "<changefreq>daily</changefreq>";
      echo "<priority>1</priority>";
    echo "</url>";

    $statement = $conexion->prepare("SELECT * FROM retrocompatibles_360 ORDER BY Agregado DESC");
    $statement->execute();
    $ultimo_juego_360 = $statement->fetchAll();

    echo "<url>";
      echo "<loc>http://www.laxtore.com/Juegos/Xbox-360/</loc>";
      echo "<lastmod>" . substr($ultimo_juego_360[0]['Agregado'],0,10) . "</lastmod>";
      echo "<changefreq>daily</changefreq>";
      echo "<priority>1</priority>";
    echo "</url>";

    $statement = $conexion->prepare("SELECT * FROM retrocompatibles_360 WHERE retro = 1 ORDER BY Agregado DESC");
    $statement->execute();
    $ultimo_juego_360_retro = $statement->fetchAll();

    echo "<url>";
      echo "<loc>http://www.laxtore.com/Juegos/Xbox-One/retrocompatibles/</loc>";
      echo "<lastmod>" . substr($ultimo_juego_360_retro[0]['Agregado'],0,10) . "</lastmod>";
      echo "<changefreq>monthly</changefreq>";
      echo "<priority>1</priority>";
    echo "</url>";

    echo "<url>";
      echo "<loc>http://www.laxtore.com/Juegos/Deals-With-Gold/</loc>";
      echo "<changefreq>weekly</changefreq>";
      echo "<priority>1</priority>";
    echo "</url>";


    $statement = $conexion->prepare("SELECT * FROM art ORDER BY fecha_publicacion");
    $statement->execute();
    $articulos = $statement->fetchAll();

    foreach ($articulos as $articulo) {
        echo "<url>";
          echo "<loc>http://www.laXtore.com/noticia/" . $articulo['ID'] . "/" . limpia_url($articulo['titulo']) . "/</loc>";
          echo "<news:news>";
            echo "<news:publication>";
              echo "<news:name>LaXtore</news:name>";
              echo "<news:language>es</news:language>";
            echo "</news:publication>";
            echo "<news:genres>TecnologĂ­a, Xbox One, videojuegos, videogames</news:genres>";
            echo "<news:publication_date>" . substr($articulo['fecha_publicacion'],0,10) . "</news:publication_date>";
            echo "<news:title>" . $articulo['titulo'] . "</news:title>";
            echo "<news:keywords>" . str_replace(",", ";",$articulo['etiquetas']) . "</news:keywords>";
          echo "</news:news>";
        echo "</url>";
    }

    $statement = $conexion->prepare("SELECT * FROM info_XboxOne ORDER BY agregado DESC");
    $statement->execute();
    while ($xbox_individual = $statement->fetch()) {
        echo "<url>";
          echo "<loc>http://www.laxtore.com/Juegos/Xbox-One/" . $xbox_individual['id'] . "/" . limpia_url($xbox_individual['Juego']) . "/</loc>";
          echo "<lastmod>" . substr($xbox_individual['agregado'],0,10) . "</lastmod>";
          echo "<priority>0.8</priority>";
        echo "</url>";
    }


echo "</urlset>";

?>

I try validating the sitemap.php file here: https://webmaster.yandex.com/tools/sitemap/?tab=url&sitemapUrl=www.laXtore.com%2Fsitemap.php

I have several errors but I don’t know how to fix them. Also the last part of the code is not shown also.

I would appreciate some kind of help.

Well, what are the errors you are getting?

Entering directly to the file on the website this:

https://s23.postimg.org/rbuschwnf/Captura_de_pantalla_2017-06-13_a_las_20.52.35.png

And on the sitemap validator this:

https://s8.postimg.org/60uu7ua11/Captura_de_pantalla_2017-06-13_a_las_20.52.01.png

Finding column 51801 on line 2 is going to be rather tricky. If you put an end-of-line after each line it will make finding the error a bit easier.

You mean using <br>?

No, either "\n" or PHP_EOL.

I keep having this problem:

error on line 2 at column 75: StartTag: invalid element name

My error, I was using <\n> instead of \n . I have a problem in the line 46, column 20, which corresponds to:

echo "<news:name>LaXtore<\news:name>\n";

With the error:

StartTag: invalid element name

Shouldn’t it be

"<news:name>LaXtore</news:name>\n"

I assume news:name is valid?

According to the documentation, the date needs to be in standard W3C datetime format - YYYY-MM-DD, not YYYY-M-DD as you have it.

The errors for news:news might be down to the validator you are using not supporting that specific sitemap extension - you could try a different validator.

1 Like

I wrote a similar script quite some time ago and after much trial and error simplified the process, thus making it easier to debug… I did this by creating a function with four parameters to write the repetative <URL>...</URL> block.

Sorry, that’s true, I found it later, but it keeps telling me that is not correct.

Thanks, I have fixed it :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.