0

How can I read the enclosure url tag in a iTunes RSS using simplexml ?

FEED:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">

<channel>
<item>
   <itunes:subtitle> subtitle </itunes:subtitle>
   <enclosure url="http://www.myWe/mySound.mp3" length="624557" type="audio/mp3"/>
</item>
</channel>
</rss>

PHP:

foreach ($feed->channel->item as $item) {
    $iTunes_item = $item->children('http://www.itunes.com/dtds/podcast-1.0.dtd');

    $item_subtitle = $item->subtitle->attributes();
    $item_url = $iTunes_item->enclosure->attributes('url'); // enclosure url

    echo $item_subtitle;
    echo $item_url;
}
Nrc
  • 9,577
  • 17
  • 67
  • 114
  • 1
    rss->channel->item->enclosure->attributes()->url i think should get the url. – Matt Burrow Aug 29 '14 at 13:49
  • Why the negative point?. I have looked a lot of time in Google and simplexml documentation and could not find the answer. What is wrong about asking this here? – Nrc Aug 29 '14 at 15:06
  • http://php.net/manual/en/simplexml.examples-basic.php - next to first reading the manual, then searching in google, do not also forget to just search stackoverflow, too. -- what you call a negative point is just a downvote of the question, so to rate questions based on community review. This is common on stackoverflow, that isolated, redundant, asking how do I w/o a prepped scenario and concrete programming question, asking for tutorials / asking for tutoring assistance - why doesn't this code work walls drop - and similar get rated. – hakre Aug 31 '14 at 16:19

1 Answers1

7

To access the url use;

rss->channel->item->enclosure->attributes()->url

Try this for getting the subtitle;

$namespaces = $item->getNameSpaces(true);
$nodes = $item->children($namespaces['itunes']);
echo $nodes->subtitle;
Matt Burrow
  • 10,477
  • 2
  • 34
  • 38
  • it surprise me that it works without the itunes address. Could this be applied to for instance the tag?: For instance this does not work without the itunes address: $feed->channel->item->subtitle->attributes(); – Nrc Aug 29 '14 at 14:09
  • You already have answered well the question. But, I update the question. Do you know how to read the subtitle? It seems that the same cannot be applied? – Nrc Aug 29 '14 at 14:21
  • No it does not work with the subtitle. It seems it needs the itunes namespace – Nrc Aug 29 '14 at 14:29
  • It works very well. This is fantastic. Can you explain it a bit. I am trying to learn and search for information about this. – Nrc Aug 29 '14 at 14:54
  • Basically the element with :itunes at the top dictates a namespace. Children which inherit this namespace cannot be handled without getting use of that name space so as you found, $itunes->subtitle did not work. $item->children($namespaces['itunes']); with assigning the value of this to a variable we allow ourselves to gain access to these 'special' elements, thus allowing $nodes->subtitle. – Matt Burrow Aug 29 '14 at 14:57
  • That dance with `->getNameSpaces()` is unnecessary. If you're using at least PHP 5.2 (and if not, I pity you!), [you can use `->children('itunes', true)`](http://php.net/manual/en/simplexmlelement.children.php) to reference the namespace by its local prefix. Note that the prefix might be different in other documents (or change later) so you should really use the URI to identify it; I like to define a constant for ease of reference: `define('XMLNS_ITUNES', 'http://www.itunes.com/dtds/podcast-1.0.dtd'); /*...*/ $item->children(XMLNS_ITUNES);` – IMSoP Aug 30 '14 at 23:44