我需要使用PHP访问RSS源中的iTunes标签。我以前使用simplepie播放播客,但我不知道如何使用它来获取iTunes标签。有没有办法使用simplepie来做或有更好的方法?


好的,我尝试过Simple XML。

所有这些(下面的代码)似乎都有效

$feed = simplexml_load_file('http://sbhosting.com/feed/');
$channel = $feed->channel;
$channel_itunes = $channel->children('http://www.itunes.com/dtds/podcast-1.0.dtd');
$summary = $channel_itunes->summary;
$subtitle = $channel_itunes->subtitle;
$category = $channel_itunes->category;
$owner = $channel_itunes->owner->name;

现在我需要获得itunes类别。似乎以几种方式表现出来。 在这种情况下,我得到以下XML:

<itunes:category text="Technology"/>
<itunes:category text="Technology">
  <itunes:category text="Software How-To"/>
</itunes:category> 

我希望能够获得类似这样的类别:

$category_text = $channel_itunes->category['text'];

但这似乎不起作用。

我已经看到了其他方式来表示我真正不知道该获得的类别。

例如:

技术   商业   教育

这是媒体还是itunes还是两者兼而有之?

感谢您的帮助。 ģ

有帮助吗?

解决方案

此代码适用于我:

//$pie is a SimplePie object
$iTunesCategories=$pie->get_channel_tags(SIMPLEPIE_NAMESPACE_ITUNES,'category');
if ($iTunesCategories) {
  foreach ($iTunesCategories as $iTunesCategory) {
    $category=$iTunesCategory['attribs']['']['text'];
    $subcat=$iTunesCategory['child']["http://www.itunes.com/dtds/podcast-1.0.dtd"]['category'][0]['attribs']['']['text'];
    if ($subcat) {
      $category.=":$subcat";
    }
    //do something with $category
  }
}

其他提示

要使用SimpleXML获取属性,请改为:

$category_text = $channel_itunes->category['text'];

使用:

$category_text = $channel_itunes->category->attributes()->text;

如果你有PHP5,使用Simple XML可以帮助解析你需要的信息。

SimplePie有一个 get_item_tags()功能应该让你访问它们。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top