Very good,
I am creating an RSS feed of the news section of my website. I get the information from the database but I do not know if because I am implementing PHP incorrectly in the .xml file it is not taking the information from me. I do not even use the var_dump
commands, for example.
<?xml version="1.0" encoding="utf-8"?>
<?php header("Content-Type: application/rss+xml; charset=utf-8"); ?>
<rss version="2.0">
<channel>
<title>Noticias LaXtore</title>
<link>http://www.ejemplo.com</link>
<description>Todas las noticias relacionadas con el mundo de los videojuegos</description>
<?php
require 'admin/config.php';
try {
$conexion = new PDO($bd_config['dbname'], $bd_config['usuario'], $bd_config['password'] );
} catch (PDOException $e) {
echo "ERROR: ".$e->getMessage();
die();
}
$statement = $conexion->prepare("SELECT * FROM art ORDER BY fecha_publicacion DESC");
$statement->execute();
$posts = $statement->fetchAll();
var_dump($posts);
foreach ($posts as $post) {
echo "<item>";
echo "<title>" . $post['titulo'] . "</title>";
echo "<link>noticas.php?ID=" . $post['ID'] . "</link>";
echo "<description>" . substr($post['articulo'],300) . " </description>";
echo "</item>";
}
?>
</channel>
</rss>
I update when using another code that does work for me but it gives me problems when I get the description of the database and publish images.
<?php
// Elimina caracteres extraños que me pueden molestar en las cadenas que meto en los item de los RSS
function clrAll($str) {
$str=str_replace("&","&",$str);
$str=str_replace("'","'",$str);
$str=str_replace(">",">",$str);
$str=str_replace("<","<",$str);
return $str;
}
//creo cabeceras desde PHP para decir que devuelvo un XML
header("Content-type: text/xml");
//comienzo a escribir el código del RSS
echo '<?xml version="1.0" encoding="utf-8"?>';
//sentencia SQL para acceder a los últimos 20 artículos publicados
require '../admin/config.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();
}
$statement = $conexion->prepare("SELECT * FROM art ORDER BY fecha_publicacion DESC");
$statement->execute();
$posts = $statement->fetchAll();
//Cabeceras del RSS
echo '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">';
//Datos generales del Canal. Edítalos conforme a tus necesidades
echo "<channel>\n";
echo "<title>Noticas LaXtore</title>";
echo "<link>http://www.laXtore.com</link>";
echo "<description>Desde laXtore facilitamos a los lectores estar al tanto de todas las noticias publicadas en la web a través de una feed RSS</description>";
echo "<language>es-es</language>";
echo "<copyright>LaXtore.com</copyright>\n";
//para cada registro encontrado en la base de datos
//tengo que crear la entrada RSS en un item
foreach ($posts as $post) {
echo "<item>\n";
echo "<title>" . $post['titulo'] . "</title>\n";
echo "<description>hola</description>\n";
echo "<image>";
echo "<url>http://www.imagenesfrases.net/wp- content/uploads/2016/05/imagenes-con-frases-chistosas-para-reir-para-wasap.jpg</url>";
echo "<title>W3Schools.com</title>";
echo "<link>https://www.w3schools.com</link>";
echo "</image>";
echo "<link>http://www.laXtore.com/noticias/noticias.php?ID=" . $post["ID"] . "</link>\n";
echo "<pubDate>". $post['fecha_publicacion'] ."</pubDate>\n";
echo "</item>\n";
}
//cierro las etiquetas del XML
echo "</channel>";
echo "</rss>";
?>