Home > front end >  Converting RSS feed to json is missing pieces
Converting RSS feed to json is missing pieces

Time:02-23

I am trying to convert a RSS feed to json PHP. I get the title, description etc. but I am missing all elements starting with "itunes".

This is for instance not showing in my json:

<itunes:subtitle>Subtitle text here</itunes:subtitle>

But these are showing:

<title>Title here</title>
<description>Description here</description>

I think it has something to do with converting the XML to json, which I use this code for:

function Parse($url) {
    $fileContents= file_get_contents($url);
    $fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
    $fileContents = trim(str_replace('"', "'", $fileContents));
    $simpleXml = simplexml_load_string($fileContents);
    $json = json_encode($simpleXml);
    return $json;
}

Can anybody help me understand where the error occurs?

CodePudding user response:

Ahh you are destroying the XML with the string replacement you are doing. Instead all you need to do is convert the tags with : in them to something that will be valid in PHP once converted to JSON

So just do the str_replace() to convert

<itunes:subtitle>Subtitle text here</itunes:subtitle>

To

<itunes_subtitle>Subtitle text here</itunes_subtitle>

That will make those tags legal when converted to a PHP data structure

function Parse($url) {
    $fileContents= file_get_contents($url);
    
    $fileContents = str_replace(':', '_', $fileContents);
    
    $simpleXml = simplexml_load_string($fileContents);
    $json = json_encode($simpleXml);
    return $json;
}

RESULT

stdClass Object
(
    [@attributes] => stdClass Object
        (
            [version] => 2.0
            [xmlns_itunes] => http_//www.itunes.com/dtds/podcast-1.0.dtd
            [xmlns_atom] => http_//www.w3.org/2005/Atom
        )

    [channel] => stdClass Object
        (
            [atom_link] => stdClass Object
                (
                    [@attributes] => stdClass Object
                        (
                            [href] => https_//feeds.soundcloud.com/users/soundcloud_users_30705140/sounds.rss
                            [rel] => self
                            [type] => application/rss xml
                        )

                )

            [title] => djsksm
            [link] => https_//soundcloud.com/djsksm
            [pubDate] => Tue, 18 Dec 2012 01_53_09  0000
            [lastBuildDate] => Tue, 18 Dec 2012 01_53_09  0000
            [ttl] => 60
            [language] => en
            [copyright] => All rights reserved
            [webMaster] => [email protected] (SoundCloud Feeds)
            [description] => Podcast by djsksm
            [itunes_subtitle] => Podcast by djsksm
            [itunes_owner] => stdClass Object
                (
                    [itunes_name] => djsksm
                    [itunes_email] => [email protected]
                )

            [itunes_author] => djsksm
            [itunes_explicit] => no
            [itunes_image] => stdClass Object
                (
                    [@attributes] => stdClass Object
                        (
                            [href] => https_//a1.sndcdn.com/images/default_avatar_original.png
                        )

                )

            [image] => stdClass Object
                (
                    [url] => https_//a1.sndcdn.com/images/default_avatar_original.png
                    [title] => djsksm
                    [link] => https_//soundcloud.com/djsksm
                )

        )

)
  • Related