如何使用jQuery / jFeed从php页面解析rss?

时间:2010-03-12 17:01:02

标签: php jquery rss jfeed

我正试图通过使用jQuery和jFeed明智地解析rss来摸索我的方式。

由于same origin policy我将BBC的健康新闻Feed提取到本地页面( http://www.davidrhysthomas.co.uk/play/proxy.php)。

最初这与jFeed下载包中提供的proxy.php脚本相同,但是由于我的主机禁用allow_url_fopen()我将php修改为以下内容:

$url = "http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/health/rss.xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);

echo "$data";
curl_close($ch);

这似乎与我本地计算机上的原始fopen生成相同/可比较的内容。

现在这似乎正在起作用,我正在考虑设置jFeed脚本以使用该页面,并且令我尴尬的是,不知道如何。

我理解,至少,这应该有效:

jQuery.getFeed({
   url: 'http://www.davidrhysthomas.co.uk/play/proxy.php',
   success: function(feed) {
      alert(feed.title);
   }
});

...但是,正如我确定你预料的那样,它。您可以在此处阅读哪些非输出内容: http://www.davidrhysthomas.co.uk/play/exampleTest.html。老实说,我不知道如何处理它。

如果有人能提供一些指示,提示,提示,或者在紧要情况下,快速拍打脸颊周围并“拉扯自己!”非常感谢...

提前致谢=)

3 个答案:

答案 0 :(得分:1)

在您的测试页面上,您有一些看起来错误的脚本......

<script type="text/javascript">

jQuery(function() {

   url: 'http://www.davidrhysthomas.co.uk/play/proxy.php',
   success: function(feed) {
      alert(feed.title);
   }
...

我认为应该更像......

<script type="text/javascript">

jQuery(function() {

   jQury.ajax( {
       url: 'http://www.davidrhysthomas.co.uk/play/proxy.php',
       success: function(feed) {
           alert(feed.title);
       }
   });
...

答案 1 :(得分:1)

Zend Framework有一个用于消费各种feed的类。

它被称为Zend_Feed

http://framework.zend.com/manual/en/zend.feed.html

答案 2 :(得分:0)

在您的PHP代码中,您错过了xml标题:

$url = "http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/health/rss.xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
header("content-type: text/xml"); 
echo "$data";
curl_close($ch);