javascript 如何显示来自其他站点的 RSS 提要

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10615635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:27:16  来源:igfitidea点击:

How to display RSS feeds from other sites

phpjavascripthtmlrss

提问by ragebunny

I have been researching this topic for a few days now and i'm still non the wiser as on how to do it.

我已经研究这个话题几天了,但我仍然不知道如何去做。

I want to get an RSS feed from forexfactory.com to my website, i want to do some formatting on whats happening and i also want the latest information from them (Although those last two points can wait as long as i have some more or feed running).

我想从 forexfactory.com 获得一个 RSS 提要到我的网站,我想对正在发生的事情做一些格式化,我还想从他们那里获得最新信息(尽管最后两点可以等待,只要我有更多或提要跑步)。

Preferably I'd like to develop this from the ground up if anyone knows of a tutorial or something i could use?

如果有人知道教程或我可以使用的东西,我最好从头开始开发它?

If not i will settle for using a third party API or something like that as long as i get to do some of the work.

如果不是,我会满足于使用第三方 API 或类似的东西,只要我能做一些工作。

I'm not sure what it is but there is something about RSS that i'm not getting so if anyone knows of any good, probably basic tutorials that would help me out a lot. It's kind of hard going through page after page of google searches.

我不确定它是什么,但有一些关于 RSS 的东西我没有得到,所以如果有人知道任何好的,可能是基本的教程会对我有很大帮助。逐页浏览谷歌搜索有点困难。

Also i'm not to fussed on the language it's outputted in Javascript, PHP or HTML will be great though.

此外,我不会对它在 Javascript、PHP 或 HTML 中输出的语言大惊小怪,但会很棒。

Thanks for the help.

谢谢您的帮助。

采纳答案by Liam Spencer

It looks like SimplePie may be what you are looking for. It's a very basic RSS plugin which is quite easy to use and is customisable too. You can download it from the website.

看起来 SimplePie 可能正是您要找的。这是一个非常基本的 RSS 插件,它非常易于使用并且也可以自定义。您可以从网站下载。

You can use it at it's bare bonesor you can delve deeperin to the plugin if you wish. Here's a demoon their website.

你可以在它的使用裸露的骨头也可以深入研究,如果你想在该插件。这是他们网站上的演示

回答by Ravi Patel

index.php

索引.php

include('rss_class.php');

  $feedlist = new rss($feed_url);
  echo $feedlist->display(2,"Feed Title");

rss_class.php

rss_class.php

<?php
 class rss {
        var $feed;
        function rss($feed){
            $this->feed = $feed;
        }

        function parse(){
            $rss = simplexml_load_file($this->feed);

            //print_r($rss);die; /// Check here for attributes

            $rss_split = array();

            foreach ($rss->channel->item as $item) {

              $title = (string) $item->title; 
              $link   = (string) $item->link; 
              $pubDate   = (string) $item->pubDate; 
              $description = (string) $item->description; 
              $image = $rss->channel->item->enclosure->attributes();
              $image_url =   $image['url'];

             $rss_split[] = '
                    <li>
                        <h5><a href="'.$link.'">'.$title.'</a></h5>
                        <span class="dateWrap">'.$pubDate.'</span>
                        <p>'.$description.'</p>
                        <a href="'.$link.'">Read Full Story</a>
                    </li>
                ';
            }
            return $rss_split;
        }

        function display($numrows,$head){

            $rss_split = $this->parse();
            $i = 0;
            $rss_data = '<h2>'.$head.'</h2><ul class="newsBlock">';
            while($i<$numrows){
              $rss_data .= $rss_split[$i];
              $i++;
            }
            $trim = str_replace('', '',$this->feed);
            $user = str_replace('&lang=en-us&format=rss_200','',$trim);


            $rss_data.='</ul>';

            return $rss_data;
        }
}
?>

回答by developer68

I didn't incorporate the < TABLE > tags as there might be more than one article that you would like to display.

我没有合并 < TABLE > 标签,因为您可能想要显示不止一篇文章。

class RssFeed
{
    public $rss = "";

    public function __construct($article)
    {
      $this->rss = simplexml_load_file($article, 'SimpleXMLElement', LIBXML_NOERROR | LIBXML_NOWARNING);

      if($this->rss != false)
      {
        printf("<TR>\r\n");
        printf("<TD>\r\n");
        printf("<h3>%s</h3>\r\n", $this->rss->channel->title);
        printf("</TD></TR>\r\n");

        foreach($this->rss->channel->item as $value)
        {
          printf("<TR>\r\n");
          printf("<TD id=\"feedmiddletd\">\r\n");
          printf("<A target=\"_blank\" HREF=\"%s\">%s</A><BR/>\r\n", $value->link, $value->title);
          printf($value->description);
          printf("</TD></TR>\r\n");
        }
      }
    }
}