如何从RSS Feed获取链接?

时间:2011-10-01 14:49:01

标签: php xml

我正在尝试获取RSS源的帖子链接。我正确地加载了一个数组中的所有帖子(我成功地回显了内容和其他标签)但是我有一个问题来获取链接。

在Feed中,可以通过两种方式找到链接

1

<link rel="alternate" type="text/html" href="this is the address I want" title="here goes the title" />

并尝试<?php echo $post->link[href]; ?>,但由于内容中有很多链接标记,因此必须回显rel="alternate"

2

<feedburner:origLink>this is the address</feedburner:origLink>

并尝试了<?php echo $post->feedburner:origLink; ?>

我的问题是如何获得链接?我更喜欢第二种方式,因为它没有通过feedburner链接。

注意:我在数组中使用了两个RSS XML结构,所以我将使用的是这样的

($post->description)?$post->description:$post->content)和我的描述/内容一样

2 个答案:

答案 0 :(得分:1)

<强> 1。 rel=alternate

$links = $post->xpath('link[@rel="alternate" and @type="text/html"]');
$link  = (string) $links[0]['href'];

请参阅http://php.net/simplexmlelement.xpathhttp://php.net/simplexml.examples-basic(示例#5)

<强> 2。 feedburner:origLink

$links = $post->xpath('feedburner:origLink');
$link = (string) $links[0];
// or
$link = (string) $post->children('feedburner', TRUE)->origLink;

请参阅http://php.net/simplexmlelement.children

答案 1 :(得分:0)

我遇到了同样的问题,但我用以下方法解决了这个问题:

$link = $xml->entry[$i]->link[2]->attributes()->href; 
//the feed-blog has 3 type of links

您可能$xml $post

相关问题