I am creating an RSS file takin information from a database, but it’s empty even I am connecting to the database correctly. I don’t why is not working, but PHP commands like var_dump are not working:
<?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>" . strip_tags(substr($post['articulo'], 0, 100)) . "</description>\n";
echo "<link>" . $registro["enlace"] . "</link>\n";
echo "<pubDate>". $post['fecha_publicacion'] ."</pubDate>\n";
echo "</item>\n";
}
//cierro las etiquetas del XML
echo "</channel>";
echo "</rss>";
?>
I have edited the post because I am using know another code, in this case works, but I am having problems with the description tag. strip_tags(substr($post['articulo'], 0, 350)) is not working but I think it is because something is wrong when I take the information from the database
$registro is wrong, $post variable is the correct. I have OK in the code but I cannot edit the first post.
Deleting the RSS headers it doesn’t change anything. The only information that appears is the name of the RSS and the description. But not the items. The code of the foreach loop I am using:
If I remove the <description> tag all article titles and date information are shown in the page. I don’t understand why is this happening because the information in the variable $post['articulo'] is correct. If I inspect the webpage I can see that the description information is there, but it is not shown.
I normally use var_dump to see the information I am receiving from the database. It is the first time it is not showing me the information, but again, if I inspect the webpage, the information of the var_dump command it is shown.
OK, that suggests there is a problem with the content of the $post['articulo'] column. Does it fail with any of them, for example if you restrict the query to the first row? Or does it only fail with all of them?
The problem is that depending of the amount of letters I take from the database the output is different. For example if I try strip_tags(substr($post['articulo'], 0, 100)) I receive 7 articles, if I try strip_tags(substr($post['articulo'], 0, 50)) I receive more than 7. It might be a problem with the information in the database…The information saved in that column contains tags. <p>, <h2> and those kind of elements.
Maybe the problem is that when you split the string at an arbitrary point such as 50 or 100 characters, in some cases that might be part-way through an HTML tag. I’ve never tried strip_tags(), but perhaps it only strips out complete tags, so when you truncate one it might leave in <p because the end is missing, for example. Maybe:
substr(strip_tags($post['articulo']),0,50)
might be the way - strip the tags out, then truncate. It might take longer, if it becomes noticeable you could always revert to the other way around and write a custom version of substr to make sure it only splits outside a tag.
It keeps not working. The only thing I think may work is to write an extract of every article. I use strip_tags to pre visualize the content of the articles, but I don’t understand why in this case it is not working.
Sorry, this is what it is shown when I inspect the page:
<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"><channel>
<title>LaXtore</title><link>http://www.laXtore.com</link><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><language>es-es</language><copyright>LaXtore.com</copyright>
<item>
<title>Anunciado Just Cause 3 Gold Edition</title>
<description>Square Enix ha anunciado en el día de hoy que lanzará una edición con t</description>
<image><url>http://www.imagenesfrases.net/wp-content/uploads/2016/05/imagenes-con-frases-chistosas-para-reir-para-wasap.jpg</url><title>W3Schools.com</title><link>https://www.w3schools.com</link></image><link>http://www.laXtore.com/noticias/noticias.php?ID=63</link>
<pubDate>2017-02-16 11:03:33</pubDate>
</item>
<item>
<title>Nueva fecha para la conferencia de Xbox en el E3</title>
<description>A diferencia de otros años, Xbox presentará su conferencia en el E</description>
<image><url>http://www.imagenesfrases.net/wp-content/uploads/2016/05/imagenes-con-frases-chistosas-para-reir-para-wasap.jpg</url><title>W3Schools.com</title><link>https://www.w3schools.com</link></image><link>http://www.laXtore.com/noticias/noticias.php?ID=62</link>
<pubDate>2017-02-15 19:56:58</pubDate>
</item>
<item>
<title>Compra cualquier bundle de Xbox One S y llévate otro juego por solo 19,99€ más</title>
<description>A partir de hoy y hasta el 12 de marzo, todos aquellos que compren cualquier bundle de Xb</description>
<image><url>http://www.imagenesfrases.net/wp-content/uploads/2016/05/imagenes-con-frases-chistosas-para-reir-para-wasap.jpg</url><title>W3Schools.com</title><link>https://www.w3schools.com</link></image><link>http://www.laXtore.com/noticias/noticias.php?ID=61</link>
<pubDate>2017-02-14 14:47:09</pubDate>
</item>
<item>
<title>Ya disponibiles los Deals With Gold hasta el 20 de febrero</title>
<description>Como cada semana os traemos en la mayor brevedad posible las ofertas semanales que nos brinda Mic</description>
<image><url>http://www.imagenesfrases.net/wp-content/uploads/2016/05/imagenes-con-frases-chistosas-para-reir-para-wasap.jpg</url><title>W3Schools.com</title><link>https://www.w3schools.com</link></image><link>http://www.laXtore.com/noticias/noticias.php?ID=60</link>
<pubDate>2017-02-14 04:10:35</pubDate>
</item>
<item>
<title>Xbox España celebra una nueva edición del “Mes del jugón”</title>
<description>Del 13 de febrero al 12 de marzo, los usuarios podrán conseguir descuentos en la compra de</description>
<image><url>http://www.imagenesfrases.net/wp-content/uploads/2016/05/imagenes-con-frases-chistosas-para-reir-para-wasap.jpg</url><title>W3Schools.com</title><link>https://www.w3schools.com</link></image><link>http://www.laXtore.com/noticias/noticias.php?ID=59</link>
<pubDate>2017-02-13 16:20:52</pubDate>
</item>
<item>
<title>Detallado el contenido del pase de temporada de Sniper Elite 4</title>
<description>Mañana, 14 de febrero, la cuarta entrega del francotirador más conocido de los vide</description>
<image><url>http://www.imagenesfrases.net/wp-content/uploads/2016/05/imagenes-con-frases-chistosas-para-reir-para-wasap.jpg</url><title>W3Schools.com</title><link>https://www.w3schools.com</link></image><link>http://www.laXtore.com/noticias/noticias.php?ID=58</link>
<pubDate>2017-02-13 14:24:53</pubDate>
</item>
<item>
<title>Digital Foundry analiza el rendimiento de Grand Theft Auto IV en Xbox One</title>
<description>Como viene siendo habitual en los juegos de Xbox 360 que acaban aterrizando en el catálogo</description>
<image><url>http://www.imagenesfrases.net/wp-content/uploads/2016/05/imagenes-con-frases-chistosas-para-reir-para-wasap.jpg</url><title>W3Schools.com</title><link>https://www.w3schools.com</link></image><link>http://www.laXtore.com/noticias/noticias.php?ID=57</link>
<pubDate>2017-02-12 16:03:33</pubDate>
</item>
<item>
<title>Ubisoft muestra la presonalización de For Honor en un nuevo video</title>
<description>En el día de hoy Ubisoft ha publicado un video en el que podemos ver la personalizaci&oacu</description>
<image><url>http://www.imagenesfrases.net/wp-content/uploads/2016/05/imagenes-con-frases-chistosas-para-reir-para-wasap.jpg</url><title>W3Schools.com</title><link>https://www.w3schools.com</link></image><link>http://www.laXtore.com/noticias/noticias.php?ID=56</link>
<pubDate>2017-02-10 22:56:50</pubDate>
</item>
<item>
<title>Rebellion presenta el tráiler de lanzamiento de Sniper Elite 4</title>
<description>A 4 días de su lanzamiento oficial, 14 de febrero de 2017, Rebellion nos muestra el traile</description>
<image><url>http://www.imagenesfrases.net/wp-content/uploads/2016/05/imagenes-con-frases-chistosas-para-reir-para-wasap.jpg</url><title>W3Schools.com</title><link>https://www.w3schools.com</link></image><link>http://www.laXtore.com/noticias/noticias.php?ID=55</link>
<pubDate>2017-02-10 22:55:13</pubDate>
</item>
<item>
<title>Hot Wheels llega a Rocket League</title>
<description>Los coches de Hot Wheels llegarán a Rocket League el 21 de febrero, trans</description>
<image><url>http://www.imagenesfrases.net/wp-content/uploads/2016/05/imagenes-con-frases-chistosas-para-reir-para-wasap.jpg</url><title>W3Schools.com</title><link>https://www.w3schools.com</link></image><link>http://www.laXtore.com/noticias/noticias.php?ID=54</link>
<pubDate>2017-02-10 13:46:20</pubDate>
</item>
<item>
<title>Focus Home Interactive y Spiders revelan el primer video de Greedfall</title>
<description>Greedfall fue anunciado la semana pasada en un evento. Desarrollado por Spiders, es un juego RPG </description>
<image><url>http://www.imagenesfrases.net/wp-content/uploads/2016/05/imagenes-con-frases-chistosas-para-reir-para-wasap.jpg</url><title>W3Schools.com</title><link>https://www.w3schools.com</link></image><link>http://www.laXtore.com/noticias/noticias.php?ID=53</link>
<pubDate>2017-02-09 22:31:22</pubDate>
</item>
<item>
<title>Todo el contenido de Blood Bowl 2 estará disponible en la “Legendary Edition”</title>
<description>Blood Bowl 2, la secuela de la adaptación de videojuegos del clásico juego de mesa </description>
<image><url>http://www.imagenesfrases.net/wp-content/uploads/2016/05/imagenes-con-frases-chistosas-para-reir-para-wasap.jpg</url><title>W3Schools.com</title><link>https://www.w3schools.com</link></image><link>http://www.laXtore.com/noticias/noticias.php?ID=51</link>
<pubDate>2017-02-09 22:25:46</pubDate>
</item>
So the final one in the code above, is displayed correctly? Should there be one after that? If there should, what is the text in there? As stored in the database, not after it’s been processed and output.