Search code examples
phpjsonxmlitunes

How to parse xml itunes under item tag in rssfeed in php


I want to parse itunes:image::href="#" from xml rss feed to json in php. I am able to get the title, PubDate etc. But unable to parse itunes..Please help here is my rssfeed snippet.

 <?xml version='1.0' encoding='UTF-8'?>
<rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:atom="http://www.w3.org/2005/Atom">
      <channel>
        <atom:link href="http://feeds.soundcloud.com/users/soundcloud:users:61234965/sounds.rss" rel="self" type="application/rss+xml"/>
        <atom:link href="http://feeds.soundcloud.com/users/soundcloud:users:61234965/sounds.rss?before=288739617" rel="next" type="application/rss+xml"/>
        <title>The Safina Society Podcast</title>
        <link>http://www.safinasociety.org</link>
        <pubDate>Mon, 05 Mar 2018 02:43:02 +0000</pubDate>
        <lastBuildDate>Mon, 05 Mar 2018 02:43:02 +0000</lastBuildDate>
        <ttl>60</ttl>
        <language>en</language>
        <copyright>All rights reserved</copyright>
        <webMaster>[email protected] (SoundCloud Feeds)</webMaster>
        <description>This life is but a transitory test, and the ultimate success in this life is reaching its end having fulfilled Allah’s commands with a good heart and avoided His major prohibitions. At Safina Society we seek to offer the faithful the knowledge and companionship that will assist in traversing the test of life and reaching its end safely. 

It is our hope that these podcasts, classes and gatherings will spark a movement, of renewed intention, renewed study, renewed commitment to our sacred law, as well as renewed love of our Lord.</description>
        <itunes:subtitle>This life is but a transitory test, and the ultim…</itunes:subtitle>
        <itunes:owner>
          <itunes:name>Safina Society</itunes:name>
          <itunes:email>[email protected]</itunes:email>
        </itunes:owner>
        <itunes:author>Safina Society</itunes:author>
        <itunes:explicit>no</itunes:explicit>
        <itunes:image href="http://i1.sndcdn.com/avatars-000285408508-ih0xcy-original.jpg"/>
        <image>
          <url>http://i1.sndcdn.com/avatars-000285408508-ih0xcy-original.jpg</url>
          <title>Safina Society</title>
          <link>http://www.safinasociety.org</link>
        </image>
        <itunes:category text="Education"/>
        <item>
      <guid isPermaLink="false">tag:soundcloud,2010:tracks/408792456</guid>
      <title>S2:E13 - Water your Sun Plant</title>
      <pubDate>Mon, 05 Mar 2018 14:23:30 +0000</pubDate>
      <link>https://soundcloud.com/safina-society/03-01-18a</link>
      <itunes:duration>01:44:20</itunes:duration>
      <itunes:author>Safina Society</itunes:author>
      <itunes:explicit>no</itunes:explicit>
      <itunes:summary>Listen as on this episode the team visits some of history as we talk about the rise of Muslim Spain, it's fall and how we should understand civilizations. We also discuss the end of times and how we should handle ourselves, the ummah, and our imaan during the end of times.</itunes:summary>
      <itunes:subtitle>Listen as on this episode the team visits some of…</itunes:subtitle>
      <description>Listen as on this episode the team visits some of history as we talk about the rise of Muslim Spain, it's fall and how we should understand civilizations. We also discuss the end of times and how we should handle ourselves, the ummah, and our imaan during the end of times.</description>
      <enclosure type="audio/mpeg" url="http://feeds.soundcloud.com/stream/408792456-safina-society-03-01-18a.mp3" length="150229913"/>
      <itunes:image href="http://i1.sndcdn.com/artworks-000311333859-zttwpu-original.jpg"/>
    </item>
      </channel>
    </rss>

i want to get itunes:image href=".....i am unable to get data with


Solution

  • As you have not declared the methods used to process the RSS feed I have made an assumption that it is DOMDocument and DOMXPath - so the trick is to use the namespace as declared in the RSS attributes by registering that / those namespaces.

    You can of couse use a url rather than a local file but as I do not know the url I could not show that - but essentially the same.

    <?php
        /*
            Assuming that a local ccopy of the RSS exists in the same directory as this script
        */
        $file=__DIR__.'/itunes.rss';
        $dom=new DOMDocument;
        $dom->load( $file );
    
        $xp=new DOMXPath( $dom );
        $xp->registerNamespace('itunes','http://www.itunes.com/dtds/podcast-1.0.dtd');
        $xp->registerNamespace('atom','http://www.w3.org/2005/Atom');
    
        $query='//item/itunes:image';
        $col=$xp->query( $query );
    
        if( $col->length > 0 ){
    
            foreach( $col as $node ) echo $node->getAttribute('href');
    
        }
    
    ?>
    

    outputs

    http://i1.sndcdn.com/artworks-000311333859-zttwpu-original.jpg
    

    With the url, gleaned from the rss itself:

    $file='http://feeds.soundcloud.com/users/soundcloud:users:61234965/sounds.rss';
    
    $dom=new DOMDocument;
    $dom->load( $file );
    
    $xp=new DOMXPath( $dom );
    $xp->registerNamespace('itunes','http://www.itunes.com/dtds/podcast-1.0.dtd');
    $xp->registerNamespace('atom','http://www.w3.org/2005/Atom');
    
    $query='//item/itunes:image';
    $col=$xp->query( $query );
    
    if( $col->length > 0 ){
    
        foreach( $col as $node ) echo $node->getAttribute('href') . '<br />';
    
    }
    

    outputs:

    http://i1.sndcdn.com/artworks-000311333859-zttwpu-original.jpg
    http://i1.sndcdn.com/artworks-000307423581-cp3yz4-original.jpg
    http://i1.sndcdn.com/artworks-000304937310-4lpxwt-original.jpg
    http://i1.sndcdn.com/artworks-000303669636-ikttvf-original.jpg
    http://i1.sndcdn.com/artworks-000303671391-egwsr9-original.jpg
    http://i1.sndcdn.com/artworks-000292343751-dga5d4-original.jpg
    http://i1.sndcdn.com/artworks-000287215952-jl1vmx-original.jpg
    http://i1.sndcdn.com/artworks-000285476606-3fn1vc-original.jpg
    http://i1.sndcdn.com/artworks-000280384667-f5o3rl-original.jpg
    http://i1.sndcdn.com/artworks-000277395593-15vyp1-original.jpg
    http://i1.sndcdn.com/avatars-000285408508-ih0xcy-origi..... etc etc
    

    Modify the XPath query to select whatever tags you are interested in and process them however you need to. For example:

    define('BR','<br />');/* for convenience */
    
    $file='http://feeds.soundcloud.com/users/soundcloud:users:61234965/sounds.rss';
    
    $dom=new DOMDocument;
    $dom->load( $file );
    
    $xp=new DOMXPath( $dom );
    $xp->registerNamespace('itunes','http://www.itunes.com/dtds/podcast-1.0.dtd');
    $xp->registerNamespace('atom','http://www.w3.org/2005/Atom');
    
    /* add additional tags to the query */
    $query='//item/itunes:image | //item/itunes:author | //item/guid  | //item/title | //item/itunes:duration | //item/enclosure';
    $col=$xp->query( $query );
    
    if( $col->length > 0 ){
    
        foreach( $col as $node ) {
    
            $tag = $node->tagName;
            $attr = $node->attributes;
            $value = $node->nodeValue;
    
            if( $attr->length > 0 ){
    
                if( $tag=='itunes:image' ) echo $node->getAttribute('href') . BR;
                if( $tag=='enclosure' ) printf('<a href="%s" target="_blank">%s</a>%s',$node->getAttribute('url'),$node->getAttribute('url'),BR);
    
            } else{
    
                printf('tag: %s ->  value: %s%s',$tag,$value,BR );
            }
        }
    }
    

    Will output:

    tag: title  ->  value: Democracy & The Future of Liberalism: With Rashid Dar<br />
    tag: itunes:duration    ->  value: 01:43:09<br />
    tag: itunes:author  ->  value: Safina Society<br />
    <a href="http://feeds.soundcloud.com/stream/412842882-safina-society-democracy-the-future-of-liberalism-with-rashid-dar.mp3" target="_blank">http://feeds.soundcloud.com/stream/412842882-safina-society-democracy-the-future-of-liberalism-with-rashid-dar.mp3</a><br />
    http://i1.sndcdn.com/artworks-000315497973-5ksx4r-original.jpg<br />
    
    tag: title  ->  value: S2:E13 - Water your Sun Plant<br />
    tag: itunes:duration    ->  value: 01:44:20<br />
    tag: itunes:author  ->  value: Safina Society<br />
    <a href="http://feeds.soundcloud.com/stream/408792456-safina-society-03-01-18a.mp3" target="_blank">http://feeds.soundcloud.com/stream/408792456-safina-society-03-01-18a.mp3</a><br />
    http://i1.sndcdn.com/artworks-000311333859-zttwpu-original.jpg
    
    ... etc
    

    I'm sure you can figure how to add the other elements to the query given the above