获取XML Feed和输出

时间:2011-10-03 00:58:10

标签: php html xml forum

您好我想获取我网站的XML Feed:

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

并以这种格式显示:

<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>

我想最好/最简单的方法是使用PHP,那么如何获取XML并将其显示在列表项中?当Feed http://example.com

时,它会显示在http://example.com/forum

我尝试了其他问题的其他答案,但似乎没有什么对我有用。

2 个答案:

答案 0 :(得分:1)

使用PHP后,您可以尝试使用SimpleXML: http://php.net/manual/en/book.simplexml.php

只需加载该URL,以便将XML文件转换为对象: http://www.php.net/manual/en/function.simplexml-load-file.php

然后,您可以使用简单的“foreach”迭代对象,以生成所需的HTML列表。

出于测试目的,并了解如何创建对象,您可以使用“print_r()”。

答案 1 :(得分:1)

您可能需要使用命令“file_get_contents”来获取远程文件的副本,以便使用PHP对其进行解析。我很惊讶这一步是必要的,因为你说你想在你的网站上显示论坛中的项目,这样你就可以将'feed'变量设置为直接链接,假设一切都在同一个域下。如果没有,这应该有效。

    $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

      }