Image dans flux rss

Résolu/Fermé
ANTHURIUMRE Messages postés 93 Date d'inscription mardi 16 septembre 2008 Statut Membre Dernière intervention 3 janvier 2015 - 7 juil. 2014 à 11:23
ANTHURIUMRE Messages postés 93 Date d'inscription mardi 16 septembre 2008 Statut Membre Dernière intervention 3 janvier 2015 - 17 juil. 2014 à 09:32
Bonjour,

J'ai mis un flux rss sur un site. En l'état il fonctionne bien sauf que je n'arrive pas à faire prendre en compte l'image accompagnant le billet lorsque celui-ci est posté sur les réseaux sociaux. Pourriez-vous m'aider s'il vous plait.
je sais que le noeud image doit se faire avec un sous noeud comme cela
<image>
<url>http://www.site.com/repertoire</url>
<link>http://www.site.com/repertoire</link>
</image>


tout en sachant que l'image change avec chaque billet.

mais je n'arrive pas à l'agencer dans mon script suivant :

<?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 noeud 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("ce qu'il faut savoir."); // 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.site.com/");
$text_link = $link->appendChild($text_link);

$title = $xml_file->createElement("title");
$title = $channel->appendChild($title);
$text_title = $xml_file->createTextNode("site.com");
$text_title = $title->appendChild($text_title);

$image=$xml_file->createElement("image");
$image=$channel->appendChild($image);
$text_image=$xml_file->createTextNode("http://www.site.com/");
$text_image=$image->appendChild($text_image);

return $channel;
}





function add_news_node(&$parent, $root, $id_billet, $auteur, $titre, $resume, $image, $date)
{
$item = $parent->createElement("item");
$item = $root->appendChild($item);

$title = $parent->createElement("title");
$title = $item->appendChild($title);
$text_title = $parent->createTextNode($titre);
$text_title = $title->appendChild($text_title);

$link = $parent->createElement("link");
$link = $item->appendChild($link);
$text_link = $parent->createTextNode("http://www.site.com/controleur/commentaire/index.php?billets=$id_billet");
$text_link = $link->appendChild($text_link);

$desc = $parent->createElement("description");
$desc = $item->appendChild($desc);
$text_desc = $parent->createTextNode($resume);
$text_desc = $desc->appendChild($text_desc);

$com = $parent->createElement("comments");
$com = $item->appendChild($com);
$text_com = $parent->createTextNode("http://www.site.com/controleur/commentaire/index.php?billets=$id_billet");
$text_com = $com->appendChild($text_com);

$author = $parent->createElement("author");
$author = $item->appendChild($author);
$text_author = $parent->createTextNode($auteur);
$text_author = $author->appendChild($text_author);

$image=$parent->createElement("image");
$image=$item->appendChild($image);
$text_image=$parent->createTextNode("http://www.site.com/controleur/commentaire/index.php?billets=$id_billet");
$text_image=$image->appendChild($text_image);

$pubdate = $parent->createElement("pubDate");
$pubdate = $item->appendChild($pubdate);
$text_date = $parent->createTextNode($date);
$text_date = $pubdate->appendChild($text_date);

$guid = $parent->createElement("guid");
$guid = $item->appendChild($guid);
$text_guid = $parent->createTextNode("http://www.site.com/controleur/commentaire/index.php?billets=$id_billet");
$text_guid = $guid->appendChild($text_guid);

$src = $parent->createElement("source");
$src = $item->appendChild($src);
$text_src = $parent->createTextNode("http://www.site.com/");
$text_src = $src->appendChild($text_src);
}


function rebuild_rss()
{
// on se connecte à la BDD

try
{
$bdd= new PDO('mysql:host=localhost;dbname=bdd','etc','pw');
$bdd->exec('SET NAMES utf8');
}
catch(Exception $e)
{
die ('Erreur:'.$e->getMessage());
}


$req = $bdd->query('SELECT id_billet, titre1, resume,image1, auteur,DATE_FORMAT(date_de_creation,\'%d/%m/%Y à %Hh%imin%ss\') AS date_de_creation_fr FROM billets ORDER BY date_de_creation DESC LIMIT 0,20');


// on crée le fichier XML
$xml_file = new DOMDocument("1.0");


$channel = init_news_rss($xml_file);


// on ajoute chaque news au fichier RSS
while( $donnees = $req->fetch() )
{
add_news_node ($xml_file, $channel, $donnees["id_billet"], $donnees["auteur"],
$donnees["titre1"], $donnees["resume"],$donnees["image1"],
$donnees["date_de_creation"]);
}

// on écrit le fichier
$xml_file->save("rss.xml");
}



rebuild_rss();



?>


Merci pour votre aide.

A voir également:

3 réponses

dans ton exemple: tu n'indiques pas le fichier image dans url mais un répertoire :
<url>http://www.site.com/repertoire</url>
au lieu de <url>http://www.site.com/repertoire/image.jpg</url>

vérifie que ton image existe et que ta fonction retourne bien une image.
0
ANTHURIUMRE Messages postés 93 Date d'inscription mardi 16 septembre 2008 Statut Membre Dernière intervention 3 janvier 2015 2
8 juil. 2014 à 19:15
Bonsoir Qalo

il est vrai que je n'ai pas mis le fichier image mais si je devais le faire pour le script qui génère le rss, comment dois-je le faire en respectant mon script. Merci pour ton aide.
0
ANTHURIUMRE Messages postés 93 Date d'inscription mardi 16 septembre 2008 Statut Membre Dernière intervention 3 janvier 2015 2
Modifié par ANTHURIUMRE le 17/07/2014 à 09:34
Bonjour à tous ou du moins à tout ceux qui liront ce message et chercheront à m'aider.

J'ai modifié mon script et ai rajouté :

$enclosure = $parent->createElement("enclosure");
$enclosure = $item->appendChild($enclosure);
$text_enclosure = $parent->createTextNode($image);
$text_enclosure = $image->appendChild($text_enclosure);

$url = $parent->createElement("url");
$url = $item->appendChild($url);
$text_url = $parent->createTextNode("http://www.site.com/repertoire/img.jpg");
$text_url = $url->appendChild($text_url);

Ce qui fait que mon script qui est le suivant :

<?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 noeud 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("ce qu'il faut savoir."); // 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.site.com/");
$text_link = $link->appendChild($text_link);

$title = $xml_file->createElement("title");
$title = $channel->appendChild($title);
$text_title = $xml_file->createTextNode("site.com");
$text_title = $title->appendChild($text_title);

return $channel;
}





function add_news_node(&$parent, $root, $id_billet, $auteur, $titre, $resume, $image, $date)
{
$item = $parent->createElement("item");
$item = $root->appendChild($item);

$title = $parent->createElement("title");
$title = $item->appendChild($title);
$text_title = $parent->createTextNode($titre);
$text_title = $title->appendChild($text_title);

$link = $parent->createElement("link");
$link = $item->appendChild($link);
$text_link = $parent->createTextNode("http://www.site.com/controleur/commentaire/index.php?billets=$id_billet");
$text_link = $link->appendChild($text_link);

$enclosure = $parent->createElement("enclosure");
$enclosure = $item->appendChild($enclosure);
$text_enclosure = $parent->createTextNode($image);
$text_enclosure = $image->appendChild($text_enclosure);

$url = $parent->createElement("url");
$url = $item->appendChild($url);
$text_url = $parent->createTextNode("http://www.site.com/repertoire/img.jpg");
$text_url = $url->appendChild($text_url);





$desc = $parent->createElement("description");
$desc = $item->appendChild($desc);
$text_desc = $parent->createTextNode($resume);
$text_desc = $desc->appendChild($text_desc);

$com = $parent->createElement("comments");
$com = $item->appendChild($com);
$text_com = $parent->createTextNode("http://www.site.com/controleur/commentaire/index.php?billets=$id_billet");
$text_com = $com->appendChild($text_com);

$author = $parent->createElement("author");
$author = $item->appendChild($author);
$text_author = $parent->createTextNode($auteur);
$text_author = $author->appendChild($text_author);

$pubdate = $parent->createElement("pubDate");
$pubdate = $item->appendChild($pubdate);
$text_date = $parent->createTextNode($date);
$text_date = $pubdate->appendChild($text_date);

$guid = $parent->createElement("guid");
$guid = $item->appendChild($guid);
$text_guid = $parent->createTextNode("http://www.site.com/controleur/commentaire/index.php?billets=$id_billet");
$text_guid = $guid->appendChild($text_guid);

$src = $parent->createElement("source");
$src = $item->appendChild($src);
$text_src = $parent->createTextNode("http://www.site.com/");
$text_src = $src->appendChild($text_src);
}


function rebuild_rss()
{
// on se connecte à la BDD

try
{
$bdd= new PDO('mysql:host=localhost;dbname=bdd','inc','pw');

}
catch(Exception $e)
{
die ('Erreur:'.$e->getMessage());
}


$req = $bdd->query('SELECT id_billet, titre1, resume,image1, auteur,DATE_FORMAT(date_de_creation,\'%d/%m/%Y à %Hh%imin%ss\') AS date_de_creation_fr FROM billets ORDER BY date_de_creation_fr DESC LIMIT 0,20');


// on crée le fichier XML
$xml_file = new DOMDocument("1.0");


$channel = init_news_rss($xml_file);


// on ajoute chaque news au fichier RSS
while( $donnees = $req->fetch() )
{
add_news_node ($xml_file, $channel, $donnees["id_billet"], $donnees["auteur"],
$donnees["titre1"], $donnees["resume"],$donnees["image1"],
$donnees["date_de_creation_fr"]);
}

// on écrit le fichier
$xml_file->save("rss.xml");
}



rebuild_rss();



?>

Il n'y a pas d'erreur qui s'affiche mais bien que le flux fonctionne correctement les images n'accompagnent toujours pas les billets.

Pourtant j'ai essayé d'adapter ce qui se dit sur blog cozic ou w3school.com.

Merci pour votre aide.
0