Manny Posted August 9, 2013 Share Posted August 9, 2013 (edited) I'm currently working on a project which involves iTunes RSS feeds. For example, this is the RSS feed for the current top 10 singles in the UK iTunes store https://itunes.apple.com/gb/rss/topsongs/limit=10/explicit=true/xml Taking a look at the code, here is an example entry from that feed: <entry> <updated>2013-08-09T09:23:18-07:00</updated> <id im:id="667931604">https://itunes.apple.com/gb/album/wake-me-up/id667931602?i=667931604&uo=2</id>'>https://itunes.apple.com/gb/album/wake-me-up/id667931602?i=667931604&uo=2</id> <title>Wake Me Up - Avicii</title> <im:name>Wake Me Up</im:name> <link rel="alternate" type="text/html" href="https://itunes.apple.com/gb/album/wake-me-up/id667931602?i=667931604&uo=2"/> <im:contentType term="Music" label="Music"><im:contentType term="Track" label="Track"/></im:contentType> <category im:id="17" term="Dance" scheme="https://itunes.apple.com/gb/genre/music-dance/id17?uo=2" label="Dance"/> <link title="Preview" rel="enclosure" type="audio/x-m4a" href="http://a1130.phobos.apple.com/us/r2000/010/Music6/v4/c2/d7/c8/c2d7c88d-9c72-fe8b-bf15-6339bd0d2ed7/mzaf_7989860748510150629.plus.aac.p.m4a" im:assetType="preview"><im:duration>30000</im:duration></link> <im:artist href="https://itunes.apple.com/gb/artist/avicii/id298496035?uo=2">Avicii</im:artist> <im:price amount="0.99000" currency="GBP">£0.99</im:price> <im:image height="55">http://a1697.phobos.apple.com/us/r2000/019/Music6/v4/41/45/93/4145934f-b7b9-76e1-f190-05b50a4588e1/13UAAIM09848.55x55-70.jpg</im:image> <im:image height="60">http://a419.phobos.apple.com/us/r2000/019/Music6/v4/41/45/93/4145934f-b7b9-76e1-f190-05b50a4588e1/13UAAIM09848.60x60-50.jpg</im:image> <im:image height="170">http://a1266.phobos.apple.com/us/r2000/019/Music6/v4/41/45/93/4145934f-b7b9-76e1-f190-05b50a4588e1/13UAAIM09848.170x170-75.jpg</im:image> <rights>℗ 2013 Avicii Music AB, under exclusive license to Universal Music AB</rights> <im:releaseDate label="25 June 2013">2013-06-25T00:00:00-07:00</im:releaseDate></entry> Having followed some advice from elsewhere - http://stackoverflow.com/questions/9965370/parse-itunes-rss-atom-feed-with-php - I have managed to get hold of all data, with the exception of one thing. Frm the line below, I need to be able to get the value of 'im:id' (i.e. 667931604) <id im:id="667931604">https://itunes.apple.com/gb/album/wake-me-up/id667931602?i=667931604&uo=2</id>'>https://itunes.apple.com/gb/album/wake-me-up/id667931602?i=667931604&uo=2</id> Here's the code I have so far: $itunes_xml = file_get_contents('https://itunes.apple.com/gb/rss/topsongs/limit=10/explicit=true/xml');// Remove the colon ":" in the <xxx:yyy> to be <xxxyyy>$itunes_xml = preg_replace("/(</?)(w+)[^>]*>)/", "$1$2$3", $itunes_xml);$itunes_xml = simplexml_load_string($itunes_xml);foreach ($itunes_xml->entry as $entry) { echo $entry->id['im:id']; // This doesn't work echo $entry->imprice['amount']; // This works} To me, it looks like the colon in 'im:id' is breaking the script. I'm aware of the preg_replace function that has been used, so maybe that is causing the issue, but if I remove it then I'm unable to retrieve other elements of the feed. Is anybody aware of a fix for this? I'd appreciate any help. Edited August 9, 2013 by Manny Link to comment Share on other sites More sharing options...
justsomeguy Posted August 9, 2013 Share Posted August 9, 2013 Use var_dump to see what $entry->id contains. http://www.php.net/manual/en/function.var-dump.php Link to comment Share on other sites More sharing options...
Manny Posted August 9, 2013 Author Share Posted August 9, 2013 var_dump returns: object(SimpleXMLElement)#4 (1) { [0]=> string(73) "https://itunes.apple.com/gb/album/wake-me-up/id667931602?i=667931604&uo=2" } That's what I expected, as that is the actual value of 'id'. It doesn't pick up the 'im'id' parameter though. <id im:id="667931604">https://itunes.apple.com/gb/album/wake-me-up/id667931602?i=667931604&uo=2</id> Link to comment Share on other sites More sharing options...
justsomeguy Posted August 9, 2013 Share Posted August 9, 2013 You probably need to use this: http://php.net/manual/en/simplexmlelement.attributes.php The namespace for your attribute is "im". Link to comment Share on other sites More sharing options...
Manny Posted August 9, 2013 Author Share Posted August 9, 2013 That's done the job. For anybody else who runs into this problem in the future, here is how to get the value of 'im:id'. <?php$itunes_feed = "https://itunes.apple.com/gb/rss/topsongs/limit=10/explicit=true/xml";$itunes_feed = file_get_contents($itunes_feed);$itunes_feed = preg_replace("/(</?)(w+)[^>]*>)/", "$1$2$3", $itunes_feed);$itunes_xml = new SimpleXMLElement($itunes_feed);$itunes_entry = $itunes_xml->entry;foreach($itunes_entry as $entry){ // Get the value of the entry ID, by using the 'im' namespace within the <id> attribute $entry_id['im'] = $entry->id->attributes('im', TRUE); echo $entry_id['im']['id'];}?> I found a little extra help over at http://stackoverflow.com/questions/12325821/getting-xml-attributes-in-php Thank you very much, justsomeguy. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now