问题
I need to get access to the iTunes tags in an RSS feed using PHP. I've used simplepie before for podcast feeds, but I'm not sure how to get the iTunes tags using it. Is there a way to use simplepie to do it or is there a better way?
Okay I tried Simple XML.
All this (the code below) seems to work
$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;
Now I need to get the itunes categories. The seem to be represented in several ways. In this case I get the follow XML:
<itunes:category text="Technology"/>
<itunes:category text="Technology">
<itunes:category text="Software How-To"/>
</itunes:category>
I would expect to be able to get the category with something like this:
$category_text = $channel_itunes->category['text'];
But that does not seem to work.
I've seen other ways to represent the category that I really don't know who to get.
For example:
Technology Business Education
Is this a media thing or a itunes thing or both?
Thanks For Your Help. G
回答1:
This code works for me:
//$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
}
}
回答2:
To get the attribute with SimpleXML, instead:
$category_text = $channel_itunes->category['text'];
Use:
$category_text = $channel_itunes->category->attributes()->text;
回答3:
If you have PHP5, using Simple XML can help in parsing the info you need.
回答4:
SimplePie has a get_item_tags() function that should let you access them.
来源:https://stackoverflow.com/questions/171230/php-access-to-itunes-tags-in-an-rss-feed