开发者

Getting XML feed and outputting

Hello I would like to get the XML feed for my site:

http://buildworx-mc.com/forum/syndication.php?fid=4&limit=5

And display them in 开发者_运维问答this format:

<ul>
   <li><a href="linktothread"> Topic 1 </a> </li>
   <li><a href="linktothread"> Topic 2 </a> </li>
   <li><a href="linktothread"> Topic 3 </a> </li>
</ul>

I guess the best/easiest method to do this is using PHP, so how can I get the XML and display them in list items? It'll be displayed on http://example.com while the feed is http://example.com/forum

I tried the other answers from other questions asked, but nothing seems to work for me.


You may need to use the command "file_get_contents" to get a copy of the remote file in order to parse it using PHP. I'm surprised this step is necessary since you say you want to display items from your forum on your site so you might be able to set the 'feed' variable to the direct link, assuming everything is under the same domain. If not, this ought to work.

    $feed = file_get_contents('http://buildworx-mc.com/forum/syndication.php?fid=4&limit=5');
    $xml = simplexml_load_string($feed);

    $items = $xml->channel->item;

    foreach($items as $item) {
      $title = $item->title;
      $link = $item->link;
      $pubDate = $item->pubDate;
      $description = $item->description;

      echo  $title . "<br>";

     // continue to format as an unordered list

      }


You may try SimpleXML once you are using PHP: http://php.net/manual/en/book.simplexml.php

Just load that URL, so that it will convert the XML file into an object: http://www.php.net/manual/en/function.simplexml-load-file.php

Then you may iterate the objects with a simple "foreach", to generate that HTML list you want.

For testing purposes, and to understand how the object is created, you may use "print_r()".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜