|
Par
Bougie
Mise à jour : 08/01/2009
Difficulté :
Intermédiaire
166 visites depuis 7 jours,
classé 451/786
|


), mais sans quelques mots de vocabulaire, vous aurez du mal à voir de quoi je parle.1 2 3 4 5 6 7 8 | <?xml version="1.0" encoding="ISO-8859-15" ?> <rss version="2.0"> <channel> <description>Une description</description> <link>Un lien</link> <title>Oh, un titre ! :D</title> </channel> </rss> |




1 2 3 4 5 6 7 8 | <?xml version="1.0" encoding="ISO-8859-15" ?> <rss version="2.0"> <channel> <description>Description du site</description> <link>Lien vers le site</link> <title>Titre du site</title> </channel> </rss> |

1 2 3 4 5 6 7 8 9 10 | <item> <title>Titre de la news</title> <link>Lien vers la news en entier</link> <description>Description de la news, le mieux étant de mettre le contenu de la news</description> <comments>Un lien vers des commentaires de news</comments> <author>Auteur de la news</author> <pubDate>Date de publication de la news</pubDate> <guid>Texte qui identifie de manière unique la news, le mieux étant de mettre le titre de la news</guid> <source>Source dont est tirée la news</source> </item> |
1 2 3 4 5 6 7 8 9 | <item> <!-- Titre, lien, etc. de la première news --> </item> <item> <!-- Titre, lien, etc. de la deuxième news --> </item> <item> <!-- etc. --> </item> |


1 2 3 4 5 6 7 8 | CREATE TABLE `news` ( `nws_id` smallint(3) UNSIGNED NOT NULL AUTO_INCREMENT, `nws_pseudo` varchar(255) NOT NULL DEFAULT '0', `nws_titre` varchar(255) NOT NULL DEFAULT '', `nws_contenu` text NOT NULL, `nws_date` int(10) NOT NULL DEFAULT '0', PRIMARY KEY `id` (`nws_id`), ) TYPE=InnoDB COMMENT='Contient les news' AUTO_INCREMENT=59 ; |

1 | SELECT nws_id, nws_pseudo, nws_titre, nws_contenu, nws_date FROM news ORDER BY nws_date DESC LIMIT 10 OFFSET 0 |
1 2 3 4 5 6 | <?php mysql_connect("localhost", "root", "mdp"); mysql_select_db("votre_table"); $nws = mysql_query("SELECT nws_id, nws_pseudo, nws_titre, nws_contenu, nws_date FROM news WHERE ORDER BY nws_date DESC LIMIT 0 OFFSET 10"); ?> |


1 2 3 | <?php $xml_file = domxml_new_doc("1.0"); ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php function &init_news_rss(&$xml_file) { $root = $xml_file->create_element("rss"); // création de l'élément $root->set_attribute("version", "2.0"); // on lui ajoute un attribut $root = $xml_file->append_child($root); // on l'insère dans le nœud parent (ici root, qui est "rss") $channel = $xml_file->create_element("channel"); $channel = $root->append_child($channel); $desc = $xml_file->create_element("description"); $desc = $channel->append_child($desc); $text_desc = $xml_file->create_text_node("Partage de connaissances en tous genres"); // on insère du texte entre les balises <description></description> $text_desc = $desc->append_child($text_desc); $link = $xml_file->create_element("link"); $link = $channel->append_child($link); $text_link = $xml_file->create_text_node("http://www.bougiemind.info"); $text_link = $link->append_child($text_link); $title = $xml_file->create_element("title"); $title = $channel->append_child($title); $text_title = $xml_file->create_text_node("Bougie'S mind"); $text_title = $title->append_child($text_title); return $channel; } ?> |

1 2 3 4 5 6 7 8 | <?xml version="1.0" encoding="ISO-8859-15" ?> <rss version="2.0"> <channel> <description>Partage de connaissances en tous genres</description> <link>http://www.bougiemind.info</link> <title>Bougie'S mind</title> </channel> </rss> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <?php function add_news_node(&$parent, $root, $id, $pseudo, $titre, $contenu, $date) { $item = $parent->create_element("item"); $item = $root->append_child($item); $title = $parent->create_element("title"); $title = $item->append_child($title); $text_title = $parent->create_text_node($titre); $text_title = $title->append_child($text_title); $link = $parent->create_element("link"); $link = $item->append_child($link); $text_link = $parent->create_text_node("http://www.bougiemind.info/rss_news".$id.".html"); $text_link = $link->append_child($text_link); $desc = $parent->create_element("description"); $desc = $item->append_child($desc); $text_desc = $parent->create_text_node($contenu); $text_desc = $desc->append_child($text_desc); $com = $parent->create_element("comments"); $com = $item->append_child($com); $text_com = $parent->create_text_node("http://www.bougiemind.info/news-11-".$id.".html"); $text_com = $com->append_child($text_com); $author = $parent->create_element("author"); $author = $item->append_child($author); $text_author = $parent->create_text_node($pseudo); $text_author = $author->append_child($text_author); $pubdate = $parent->create_element("pubDate"); $pubdate = $item->append_child($pubdate); $text_date = $parent->create_text_node($date); $text_date = $pubdate->append_child($text_date); $guid = $parent->create_element("guid"); $guid = $item->append_child($guid); $text_guid = $parent->create_text_node("http://www.bougiemind.info/rss_news".$id.".html"); $text_guid = $guid->append_child($text_guid); $src = $parent->create_element("source"); $src = $item->append_child($src); $text_src = $parent->create_text_node("http://www.bougiemind.info"); $text_src = $src->append_child($text_src); } ?> |


1 2 3 4 5 6 7 8 9 10 | <item> <title>Flux RSS des news en place</title> <link>http://www.bougiemind.info/rss_news56.html</link> <description>Voilà, le flux RSS des news est en place !</description> <comments>http://www.bougiemind.info/news-11-56.html</comments> <author>Bougie</author> <pubDate>24/04/2006 12:42</pubDate> <guid>http://www.bougiemind.info/rss_news56.html</guid> <source>http://www.bougiemind.info</source> </item> |


1 2 3 4 5 6 | <?php while($news = mysql_fetch_assoc($nws)) { add_news_node($xml_file, $channel, $news["nws_id"], $news["nws_pseudo"], $news["nws_titre"], $news["nws_contenu"], date("d/m/Y H:i", $news["nws_date"])); } ?> |

1 2 3 | <?php $xml_file->dump_file("news_FR_flux.xml"); ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php function rebuild_rss() { // on se connecte à la BDD mysql_connect("localhost", "root", "mdp"); mysql_select_db("votre_table"); // on récupère les news $nws = mysql_query("SELECT nws_id, nws_pseudo, nws_titre, nws_contenu, nws_date FROM news WHERE ORDER BY nws_date DESC LIMIT 0 OFFSET 10"); // on crée le fichier XML $xml_file = domxml_new_doc("1.0"); // on ajoute chaque news au fichier RSS while($news = mysql_fetch_assoc($nws)) { add_news_node($xml_file, $channel, $news["nws_id"], $news["nws_pseudo"], $news["nws_titre"], $news["nws_contenu"], date("d/m/Y H:i", $news["nws_date"])); } // on écrit le fichier $xml_file->dump_file("news_FR_flux.xml"); } ?> |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | <?php function &init_news_rss(&$xml_file) { $root = $xml_file->create_element("rss"); // création de l'élément $root->set_attribute("version", "2.0"); // on lui ajoute un attribut $root = $xml_file->append_child($root); // on l'insère dans le nœud parent (ici root qui est "rss") $channel = $xml_file->create_element("channel"); $channel = $root->append_child($channel); $desc = $xml_file->create_element("description"); $desc = $channel->append_child($desc); $text_desc = $xml_file->create_text_node("Partage de connaissances en tous genres"); // on insère du texte entre les balises <description></description> $text_desc = $desc->append_child($text_desc); $link = $xml_file->create_element("link"); $link = $channel->append_child($link); $text_link = $xml_file->create_text_node("http://www.bougiemind.info"); $text_link = $link->append_child($text_link); $title = $xml_file->create_element("title"); $title = $channel->append_child($title); $text_title = $xml_file->create_text_node("Bougie'S mind"); $text_title = $title->append_child($text_title); return $channel; } function add_news_node(&$parent, $root, $id, $pseudo, $titre, $contenu, $date) { $item = $parent->create_element("item"); $item = $root->append_child($item); $title = $parent->create_element("title"); $title = $item->append_child($title); $text_title = $parent->create_text_node($titre); $text_title = $title->append_child($text_title); $link = $parent->create_element("link"); $link = $item->append_child($link); $text_link = $parent->create_text_node("http://www.bougiemind.info/rss_news".$id.".html"); $text_link = $link->append_child($text_link); $desc = $parent->create_element("description"); $desc = $item->append_child($desc); $text_desc = $parent->create_text_node($contenu); $text_desc = $desc->append_child($text_desc); $com = $parent->create_element("comments"); $com = $item->append_child($com); $text_com = $parent->create_text_node("http://www.bougiemind.info/news-11-".$id.".html"); $text_com = $com->append_child($text_com); $author = $parent->create_element("author"); $author = $item->append_child($author); $text_author = $parent->create_text_node($pseudo); $text_author = $author->append_child($text_author); $pubdate = $parent->create_element("pubDate"); $pubdate = $item->append_child($pubdate); $text_date = $parent->create_text_node($date); $text_date = $pubdate->append_child($text_date); $guid = $parent->create_element("guid"); $guid = $item->append_child($guid); $text_guid = $parent->create_text_node("http://www.bougiemind.info/rss_news".$id.".html"); $text_guid = $guid->append_child($text_guid); $src = $parent->create_element("source"); $src = $item->append_child($src); $text_src = $parent->create_text_node("http://www.bougiemind.info"); $text_src = $src->append_child($text_src); } function rebuild_rss() { // on se connecte à la BDD mysql_connect("localhost", "root", "mdp"); mysql_select_db("votre_table"); // on récupère les news $nws = mysql_query("SELECT nws_id, nws_pseudo, nws_titre, nws_contenu, nws_date FROM news WHERE ORDER BY nws_date DESC LIMIT 0 OFFSET 10"); // on crée le fichier XML $xml_file = domxml_new_doc("1.0"); // on initialise le fichier XML pour le flux RSS $channel = init_news_rss($xml_file); // on ajoute chaque news au fichier RSS while($news = mysql_fetch_assoc($nws)) { add_news_node($xml_file, $channel, $news["nws_id"], $news["nws_pseudo"], $news["nws_titre"], $news["nws_contenu"], date("d/m/Y H:i", $news["nws_date"])); } // on écrit le fichier $xml_file->dump_file("news_FR_flux.xml"); } ?> |
1 2 3 4 | <?php require('flux_rss.php'); rebuild_rss(); ?> |
1 | <link rel="alternate" type="application/rss+xml" title="{Titre du flux RSS}" href="{Lien vers le flux RSS}" /> |


).1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php function &init_news_rss(&$xml_file) { $root = $xml_file->createElement("rss"); // création de l'élément $root->setAttribute("version", "2.0"); // on lui ajoute un attribut $root = $xml_file->appendChild($root); // on l'insère dans le nœud parent (ici root qui est "rss") $channel = $xml_file->createElement("channel"); $channel = $root->appendChild($channel); $desc = $xml_file->createElement("description"); $desc = $channel->appendChild($desc); $text_desc = $xml_file->createTextNode("Partage de connaissances en tous genres"); // on insère du texte entre les balises <description></description> $text_desc = $desc->appendChild($text_desc); $link = $xml_file->createElement("link"); $link = $channel->appendChild($link); $text_link = $xml_file->createTextNode("http://www.bougiemind.info"); $text_link = $link->appendChild($text_link); $title = $xml_file->createElement("title"); $title = $channel->appendChild($title); $text_title = $xml_file->createTextNode("Bougie'S mind"); $text_title = $title->appendChild($text_title); return $channel; } ?> |

1 2 3 | <?php $xml_file = domxml_new_doc("1.0"); ?> |
1 2 3 | <?php $xml_file = new DOMDocument("1.0"); ?> |
1 2 3 | <?php $xml_file->dump_file("news_FR_flux.xml"); ?> |
1 2 3 | <?php $xml_file->save("news_FR_flux.xml"); ?> |


