Adaptation code php pour parser xml
Résolu
oncletom
Messages postés
39
Date d'inscription
Statut
Membre
Dernière intervention
-
oncletom Messages postés 39 Date d'inscription Statut Membre Dernière intervention -
oncletom Messages postés 39 Date d'inscription Statut Membre Dernière intervention -
Bonjour,
Désolé de vous importuner, mais après beaucoup d'essais et erreurs, je fais une nouvelle fois appel à vous.
J'utilisais pour afficher des articles de blogger à partir du flux rss un petit script php tès pratique : rsslib.php.
Suite à l'annonce de google d'abandonner les blogs par ftp, je me replie sur wordpress pour être "maître de mon destin..." Bref.
Tout avance à peu près comme il faut.
Mais quand j'essaie d'appliquer mon petit parser rsslib.php au flux généré par wordpress je n'obtiens plus l'article complet mis en forme avec les images comme auparavant mais seulement le texte non mis en forme.
J'ai donc examiné le flux wordpress.
- Dans blogger, l'article formaté y compris les images se trouvait dans la balise <description>
- Dans word press, la balise description existe toujours mais uniquement pour le texte sans mise en forme. Le texte mis en forme avec les images sont dans une balise <content:encoded>.
J'ai donc essayé d'adapter le code rsslib.php en ajoutant (je le croyais) le code nécessaire pour traiter la balise <content:encoded>. Ca ne marche pas du tout!
ci joint le code initial de rsslib.php, et en dessous le code modifié qui ne marche pas malgré de nombreux essais.
____________________________________________________________________
CODE INITIAL
<?php
/*
RSS Extractor and Displayer
(c) 2007 Scriptol.com - Licence Mozilla 1.1.
rsslib.php
Requirements:
- PHP 5.
- A RSS feed.
Using the library:
Insert this code into the page that displays the RSS feed:
<?php
require_once("rsslib.php");
echo RSS_Display("https://www.xul.fr/rss.xml", 25);
?>
*/
$RSS_Content = array();
function RSS_Tags($item, $type)
{
$y = array();
$tnl = $item->getElementsByTagName("title");
$tnl = $tnl->item(0);
$title = $tnl->firstChild->data;
$tnl = $item->getElementsByTagName("link");
$tnl = $tnl->item(0);
$link = $tnl->firstChild->data;
$tnl = $item->getElementsByTagName("description");
$tnl = $tnl->item(0);
$description = $tnl->firstChild->data;
$y["title"] = $title;
$y["link"] = $link;
$y["CONTENT:ENCODED"] = $description;
$y["type"] = $type;
return $y;
}
function RSS_Channel($channel)
{
global $RSS_Content;
$items = $channel->getElementsByTagName("item");
// Processing channel
$y = RSS_Tags($channel, 0); // get description of channel, type 0
array_push($RSS_Content, $y);
// Processing articles
foreach($items as $item)
{
$y = RSS_Tags($item, 1); // get description of article, type 1
array_push($RSS_Content, $y);
}
}
function RSS_Retrieve($url)
{
global $RSS_Content;
$doc = new DOMDocument();
$doc->load($url);
$channels = $doc->getElementsByTagName("channel");
$RSS_Content = array();
foreach($channels as $channel)
{
RSS_Channel($channel);
}
}
function RSS_RetrieveLinks($url)
{
global $RSS_Content;
$doc = new DOMDocument();
$doc->load($url);
$channels = $doc->getElementsByTagName("channel");
$RSS_Content = array();
foreach($channels as $channel)
{
$items = $channel->getElementsByTagName("item");
foreach($items as $item)
{
$y = RSS_Tags($item, 1); // get description of article, type 1
array_push($RSS_Content, $y);
}
}
}
function RSS_Links($url, $size)
{
global $RSS_Content;
$page = "";
RSS_RetrieveLinks($url);
if($size > 0)
$recents = array_slice($RSS_Content, 0, $size);
foreach($recents as $article)
{
$type = $article["type"];
if($type == 0) continue;
$title = $article["title"];
$link = $article["link"];
$page .= "<li><a href=\"$link\">$title</a></li>\n";
}
$page .="</ul>\n";
return $page;
}
function RSS_Display($url, $size)
{
global $RSS_Content;
$opened = false;
$page = "";
RSS_Retrieve($url);
if($size > 0)
$recents = array_slice($RSS_Content, 0, $size);
foreach($recents as $article)
{
$type = $article["type"];
if($type == 0)
{
if($opened == true)
{
$page .="</ul>\n";
$opened = false;
}
$page .="<b>";
}
else
{
if($opened == false)
{
$page .= "<ul>\n";
$opened = true;
}
}
$title = $article["title"];
$link = $article["link"];
$description = $article["description"];
$page .= "<li><a href=\"$link\">$title</a>";
if($description != false)
{
$page .= "<br>$description";
}
$page .= "</li>\n";
if($type==0)
{
$page .="</b><br />";
}
}
if($opened == true)
{
$page .="</ul>\n";
}
return $page."\n";
}
function RSS_Descr($url, $size)
{
global $RSS_Content;
$page = "";
RSS_RetrieveLinks($url);
if($size > 0)
$recents = array_slice($RSS_Content, 0, $size);
foreach($recents as $article)
{
$type = $article["type"];
if($type == 0) continue;
$link = $article["link"];
$description = $article["description"];
$page .= "$description ";
}
$page .="</ul>\n";
return $page;
}
?>
____________________________________________________________________________
et voici le code modifié, qui fonctionne toujours avec l'appel RSS_Descr mais pas du tout avec le rajout de <centent:code> et de la fonction RSS_Contcode
_____________________________________________________________________________________
CODE MODIFIE QUI NE FONCTIONNE PAS POUR CONTENT:ENCODED
<?php
/*
RSS Extractor and Displayer
(c) 2007 Scriptol.com - Licence Mozilla 1.1.
rsslib.php
Requirements:
- PHP 5.
- A RSS feed.
Using the library:
Insert this code into the page that displays the RSS feed:
<?php
require_once("rsslib.php");
echo RSS_Display("https://www.xul.fr/rss.xml", 25);
?>
*/
$RSS_Content = array();
function RSS_Tags($item, $type)
{
$y = array();
$tnl = $item->getElementsByTagName("title");
$tnl = $tnl->item(0);
$title = $tnl->firstChild->data;
$tnl = $item->getElementsByTagName("link");
$tnl = $tnl->item(0);
$link = $tnl->firstChild->data;
$tnl = $item->getElementsByTagName("description");
$tnl = $tnl->item(0);
$description = $tnl->firstChild->data;
$tnl = $item->getElementsByTagName("content:encoded");
$tnl = $tnl->item(0);
$contcode = $tnl->firstChild->data;
$y["title"] = $title;
$y["link"] = $link;
$y["description"] = $description;
$y["type"] = $type;
$y["contcode"] = $contcode;
return $y;
}
function RSS_Channel($channel)
{
global $RSS_Content;
$items = $channel->getElementsByTagName("item");
// Processing channel
$y = RSS_Tags($channel, 0); // get description of channel, type 0
array_push($RSS_Content, $y);
// Processing articles
foreach($items as $item)
{
$y = RSS_Tags($item, 1); // get description of article, type 1
array_push($RSS_Content, $y);
}
}
function RSS_Retrieve($url)
{
global $RSS_Content;
$doc = new DOMDocument();
$doc->load($url);
$channels = $doc->getElementsByTagName("channel");
$RSS_Content = array();
foreach($channels as $channel)
{
RSS_Channel($channel);
}
}
function RSS_RetrieveLinks($url)
{
global $RSS_Content;
$doc = new DOMDocument();
$doc->load($url);
$channels = $doc->getElementsByTagName("channel");
$RSS_Content = array();
foreach($channels as $channel)
{
$items = $channel->getElementsByTagName("item");
foreach($items as $item)
{
$y = RSS_Tags($item, 1); // get description of article, type 1
array_push($RSS_Content, $y);
}
}
}
function RSS_Links($url, $size)
{
global $RSS_Content;
$page = "";
RSS_RetrieveLinks($url);
if($size > 0)
$recents = array_slice($RSS_Content, 0, $size);
foreach($recents as $article)
{
$type = $article["type"];
if($type == 0) continue;
$title = $article["title"];
$link = $article["link"];
$page .= "<li><a href=\"$link\">$title</a></li>\n";
}
$page .="</ul>\n";
return $page;
}
function RSS_Display($url, $size)
{
global $RSS_Content;
$opened = false;
$page = "";
RSS_Retrieve($url);
if($size > 0)
$recents = array_slice($RSS_Content, 0, $size);
foreach($recents as $article)
{
$type = $article["type"];
if($type == 0)
{
if($opened == true)
{
$page .="</ul>\n";
$opened = false;
}
$page .="<b>";
}
else
{
if($opened == false)
{
$page .= "<ul>\n";
$opened = true;
}
}
$title = $article["title"];
$link = $article["link"];
$description = $article["description"];
$page .= "<li><a href=\"$link\">$title</a>";
if($description != false)
{
$page .= "<br>$description";
}
$page .= "</li>\n";
if($type==0)
{
$page .="</b><br />";
}
}
if($opened == true)
{
$page .="</ul>\n";
}
return $page."\n";
}
function RSS_Descr($url, $size)
{
global $RSS_Content;
$page = "";
RSS_RetrieveLinks($url);
if($size > 0)
$recents = array_slice($RSS_Content, 0, $size);
foreach($recents as $article)
{
$type = $article["type"];
if($type == 0) continue;
$link = $article["link"];
$description = $article["description"];
$page .= "$description ";
}
$page .="</ul>\n";
return $page;
}
function RSS_Contcode($url, $size)
{
global $RSS_Content;
$page = "";
RSS_RetrieveLinks($url);
if($size > 0)
$recents = array_slice($RSS_Content, 0, $size);
foreach($recents as $article)
{
$type = $article["type"];
if($type == 0) continue;
$link = $article["link"];
$description = $article["contcode"];
$page .= "$contcode ";
}
$page .="</ul>\n";
return $page;
}
?>
_________________________________________________________________________
Pour info, le flux est ici
http://www.parisfotos.com/wp/feed/
Merci beaucoup aux experts qui pourront me dire pourquoi mon code ne marche pas.
Désolé de vous importuner, mais après beaucoup d'essais et erreurs, je fais une nouvelle fois appel à vous.
J'utilisais pour afficher des articles de blogger à partir du flux rss un petit script php tès pratique : rsslib.php.
Suite à l'annonce de google d'abandonner les blogs par ftp, je me replie sur wordpress pour être "maître de mon destin..." Bref.
Tout avance à peu près comme il faut.
Mais quand j'essaie d'appliquer mon petit parser rsslib.php au flux généré par wordpress je n'obtiens plus l'article complet mis en forme avec les images comme auparavant mais seulement le texte non mis en forme.
J'ai donc examiné le flux wordpress.
- Dans blogger, l'article formaté y compris les images se trouvait dans la balise <description>
- Dans word press, la balise description existe toujours mais uniquement pour le texte sans mise en forme. Le texte mis en forme avec les images sont dans une balise <content:encoded>.
J'ai donc essayé d'adapter le code rsslib.php en ajoutant (je le croyais) le code nécessaire pour traiter la balise <content:encoded>. Ca ne marche pas du tout!
ci joint le code initial de rsslib.php, et en dessous le code modifié qui ne marche pas malgré de nombreux essais.
____________________________________________________________________
CODE INITIAL
<?php
/*
RSS Extractor and Displayer
(c) 2007 Scriptol.com - Licence Mozilla 1.1.
rsslib.php
Requirements:
- PHP 5.
- A RSS feed.
Using the library:
Insert this code into the page that displays the RSS feed:
<?php
require_once("rsslib.php");
echo RSS_Display("https://www.xul.fr/rss.xml", 25);
?>
*/
$RSS_Content = array();
function RSS_Tags($item, $type)
{
$y = array();
$tnl = $item->getElementsByTagName("title");
$tnl = $tnl->item(0);
$title = $tnl->firstChild->data;
$tnl = $item->getElementsByTagName("link");
$tnl = $tnl->item(0);
$link = $tnl->firstChild->data;
$tnl = $item->getElementsByTagName("description");
$tnl = $tnl->item(0);
$description = $tnl->firstChild->data;
$y["title"] = $title;
$y["link"] = $link;
$y["CONTENT:ENCODED"] = $description;
$y["type"] = $type;
return $y;
}
function RSS_Channel($channel)
{
global $RSS_Content;
$items = $channel->getElementsByTagName("item");
// Processing channel
$y = RSS_Tags($channel, 0); // get description of channel, type 0
array_push($RSS_Content, $y);
// Processing articles
foreach($items as $item)
{
$y = RSS_Tags($item, 1); // get description of article, type 1
array_push($RSS_Content, $y);
}
}
function RSS_Retrieve($url)
{
global $RSS_Content;
$doc = new DOMDocument();
$doc->load($url);
$channels = $doc->getElementsByTagName("channel");
$RSS_Content = array();
foreach($channels as $channel)
{
RSS_Channel($channel);
}
}
function RSS_RetrieveLinks($url)
{
global $RSS_Content;
$doc = new DOMDocument();
$doc->load($url);
$channels = $doc->getElementsByTagName("channel");
$RSS_Content = array();
foreach($channels as $channel)
{
$items = $channel->getElementsByTagName("item");
foreach($items as $item)
{
$y = RSS_Tags($item, 1); // get description of article, type 1
array_push($RSS_Content, $y);
}
}
}
function RSS_Links($url, $size)
{
global $RSS_Content;
$page = "";
RSS_RetrieveLinks($url);
if($size > 0)
$recents = array_slice($RSS_Content, 0, $size);
foreach($recents as $article)
{
$type = $article["type"];
if($type == 0) continue;
$title = $article["title"];
$link = $article["link"];
$page .= "<li><a href=\"$link\">$title</a></li>\n";
}
$page .="</ul>\n";
return $page;
}
function RSS_Display($url, $size)
{
global $RSS_Content;
$opened = false;
$page = "";
RSS_Retrieve($url);
if($size > 0)
$recents = array_slice($RSS_Content, 0, $size);
foreach($recents as $article)
{
$type = $article["type"];
if($type == 0)
{
if($opened == true)
{
$page .="</ul>\n";
$opened = false;
}
$page .="<b>";
}
else
{
if($opened == false)
{
$page .= "<ul>\n";
$opened = true;
}
}
$title = $article["title"];
$link = $article["link"];
$description = $article["description"];
$page .= "<li><a href=\"$link\">$title</a>";
if($description != false)
{
$page .= "<br>$description";
}
$page .= "</li>\n";
if($type==0)
{
$page .="</b><br />";
}
}
if($opened == true)
{
$page .="</ul>\n";
}
return $page."\n";
}
function RSS_Descr($url, $size)
{
global $RSS_Content;
$page = "";
RSS_RetrieveLinks($url);
if($size > 0)
$recents = array_slice($RSS_Content, 0, $size);
foreach($recents as $article)
{
$type = $article["type"];
if($type == 0) continue;
$link = $article["link"];
$description = $article["description"];
$page .= "$description ";
}
$page .="</ul>\n";
return $page;
}
?>
____________________________________________________________________________
et voici le code modifié, qui fonctionne toujours avec l'appel RSS_Descr mais pas du tout avec le rajout de <centent:code> et de la fonction RSS_Contcode
_____________________________________________________________________________________
CODE MODIFIE QUI NE FONCTIONNE PAS POUR CONTENT:ENCODED
<?php
/*
RSS Extractor and Displayer
(c) 2007 Scriptol.com - Licence Mozilla 1.1.
rsslib.php
Requirements:
- PHP 5.
- A RSS feed.
Using the library:
Insert this code into the page that displays the RSS feed:
<?php
require_once("rsslib.php");
echo RSS_Display("https://www.xul.fr/rss.xml", 25);
?>
*/
$RSS_Content = array();
function RSS_Tags($item, $type)
{
$y = array();
$tnl = $item->getElementsByTagName("title");
$tnl = $tnl->item(0);
$title = $tnl->firstChild->data;
$tnl = $item->getElementsByTagName("link");
$tnl = $tnl->item(0);
$link = $tnl->firstChild->data;
$tnl = $item->getElementsByTagName("description");
$tnl = $tnl->item(0);
$description = $tnl->firstChild->data;
$tnl = $item->getElementsByTagName("content:encoded");
$tnl = $tnl->item(0);
$contcode = $tnl->firstChild->data;
$y["title"] = $title;
$y["link"] = $link;
$y["description"] = $description;
$y["type"] = $type;
$y["contcode"] = $contcode;
return $y;
}
function RSS_Channel($channel)
{
global $RSS_Content;
$items = $channel->getElementsByTagName("item");
// Processing channel
$y = RSS_Tags($channel, 0); // get description of channel, type 0
array_push($RSS_Content, $y);
// Processing articles
foreach($items as $item)
{
$y = RSS_Tags($item, 1); // get description of article, type 1
array_push($RSS_Content, $y);
}
}
function RSS_Retrieve($url)
{
global $RSS_Content;
$doc = new DOMDocument();
$doc->load($url);
$channels = $doc->getElementsByTagName("channel");
$RSS_Content = array();
foreach($channels as $channel)
{
RSS_Channel($channel);
}
}
function RSS_RetrieveLinks($url)
{
global $RSS_Content;
$doc = new DOMDocument();
$doc->load($url);
$channels = $doc->getElementsByTagName("channel");
$RSS_Content = array();
foreach($channels as $channel)
{
$items = $channel->getElementsByTagName("item");
foreach($items as $item)
{
$y = RSS_Tags($item, 1); // get description of article, type 1
array_push($RSS_Content, $y);
}
}
}
function RSS_Links($url, $size)
{
global $RSS_Content;
$page = "";
RSS_RetrieveLinks($url);
if($size > 0)
$recents = array_slice($RSS_Content, 0, $size);
foreach($recents as $article)
{
$type = $article["type"];
if($type == 0) continue;
$title = $article["title"];
$link = $article["link"];
$page .= "<li><a href=\"$link\">$title</a></li>\n";
}
$page .="</ul>\n";
return $page;
}
function RSS_Display($url, $size)
{
global $RSS_Content;
$opened = false;
$page = "";
RSS_Retrieve($url);
if($size > 0)
$recents = array_slice($RSS_Content, 0, $size);
foreach($recents as $article)
{
$type = $article["type"];
if($type == 0)
{
if($opened == true)
{
$page .="</ul>\n";
$opened = false;
}
$page .="<b>";
}
else
{
if($opened == false)
{
$page .= "<ul>\n";
$opened = true;
}
}
$title = $article["title"];
$link = $article["link"];
$description = $article["description"];
$page .= "<li><a href=\"$link\">$title</a>";
if($description != false)
{
$page .= "<br>$description";
}
$page .= "</li>\n";
if($type==0)
{
$page .="</b><br />";
}
}
if($opened == true)
{
$page .="</ul>\n";
}
return $page."\n";
}
function RSS_Descr($url, $size)
{
global $RSS_Content;
$page = "";
RSS_RetrieveLinks($url);
if($size > 0)
$recents = array_slice($RSS_Content, 0, $size);
foreach($recents as $article)
{
$type = $article["type"];
if($type == 0) continue;
$link = $article["link"];
$description = $article["description"];
$page .= "$description ";
}
$page .="</ul>\n";
return $page;
}
function RSS_Contcode($url, $size)
{
global $RSS_Content;
$page = "";
RSS_RetrieveLinks($url);
if($size > 0)
$recents = array_slice($RSS_Content, 0, $size);
foreach($recents as $article)
{
$type = $article["type"];
if($type == 0) continue;
$link = $article["link"];
$description = $article["contcode"];
$page .= "$contcode ";
}
$page .="</ul>\n";
return $page;
}
?>
_________________________________________________________________________
Pour info, le flux est ici
http://www.parisfotos.com/wp/feed/
Merci beaucoup aux experts qui pourront me dire pourquoi mon code ne marche pas.
A voir également:
- Adaptation code php pour parser xml
- Code ascii - Guide
- Xml download - Télécharger - Édition & Programmation
- Code puk bloqué - Guide
- Code activation windows 10 - Guide
- Comment déverrouiller un téléphone quand on a oublié le code - Guide
1 réponse
J'ai trouvé ma réponse ici
http://www.memwg.com/enabling-wordpress-full-feeds/
iL supprime la fabrication de <content:encoded> et le remplace par <description>
Plus de problème pour le lire et mon petit parser rsslib.php fait très bien son boulot.
attention, il y a une petite erreur dans la solution décrite dan shttp://www.memwg.com/enabling-wordpress-full-feeds/
dans le fichier includes/feed-rss2.php
il faut changer le bout de code
<?php if (get_option('rss_use_excerpt')) : ?>
<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
<?php else : ?>
<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
<?php if ( strlen( $post->post_content ) > 0 ) : ?>
<content:encoded><![CDATA[<?php the_content() ?>]]></content:encoded>
<?php else : ?>
<content:encoded><![CDATA[<?php the_excerpt_rss() ?>]]>
</content:encoded>
<?php endif; ?>
<?php endif; ?>
par le code
<?php if (get_option('rss_use_excerpt')) : ?>
<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
<?php else : ?>
<description><![CDATA[<?php the_content('',0,'') ?>]]></description>
<?php endif; ?>
Mais attention un seul <?php endif; ?>
et pas deux comme indiqué dans le post cité plus haut
voilà je me suis dépanné tout seul mais ça en valait la peine.
Bonsoir à tous
http://www.memwg.com/enabling-wordpress-full-feeds/
iL supprime la fabrication de <content:encoded> et le remplace par <description>
Plus de problème pour le lire et mon petit parser rsslib.php fait très bien son boulot.
attention, il y a une petite erreur dans la solution décrite dan shttp://www.memwg.com/enabling-wordpress-full-feeds/
dans le fichier includes/feed-rss2.php
il faut changer le bout de code
<?php if (get_option('rss_use_excerpt')) : ?>
<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
<?php else : ?>
<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
<?php if ( strlen( $post->post_content ) > 0 ) : ?>
<content:encoded><![CDATA[<?php the_content() ?>]]></content:encoded>
<?php else : ?>
<content:encoded><![CDATA[<?php the_excerpt_rss() ?>]]>
</content:encoded>
<?php endif; ?>
<?php endif; ?>
par le code
<?php if (get_option('rss_use_excerpt')) : ?>
<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
<?php else : ?>
<description><![CDATA[<?php the_content('',0,'') ?>]]></description>
<?php endif; ?>
Mais attention un seul <?php endif; ?>
et pas deux comme indiqué dans le post cité plus haut
voilà je me suis dépanné tout seul mais ça en valait la peine.
Bonsoir à tous