【问题标题】:Getting XML feed and outputting获取 XML 提要和输出
【发布时间】:2011-10-03 00:58:09
【问题描述】:

您好,我想为我的网站获取 XML 提要:

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 并将它们显示在列表项中?它将显示在http://example.com 而提要为http://example.com/forum

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

【问题讨论】:

    标签: php html xml forum


    【解决方案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
    
          }
    

    【讨论】:

      【解决方案2】:

      您可以在使用 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()”。

      【讨论】:

        猜你喜欢
        • 2014-05-20
        • 2018-12-23
        • 1970-01-01
        • 1970-01-01
        • 2021-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-24
        相关资源
        最近更新 更多