Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7732615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:46:29+00:00 2026-06-01T06:46:29+00:00

Trying to parse an iTunes Atom feed with a PHP script. If you visit

  • 0

Trying to parse an iTunes Atom feed with a PHP script. If you visit the iTunes RSS Generator, you can generate an Atom feed like this:

http://itunes.apple.com/us/rss/topsongs/limit=10/genre=16/explicit=true/xml

which gives an iTunes RSS feed result like this:

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:im="http://itunes.apple.com/rss" xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <id>http://itunes.apple.com/us/rss/topsongs/limit=10/genre=16/explicit=true/xml</id><title>iTunes Store: Top Songs in Soundtrack</title><updated>2012-04-01T07:22:41-07:00</updated><link rel="alternate" type="text/html" href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewTop?id=17&amp;popId=1"/><link rel="self" href="http://itunes.apple.com/us/rss/topsongs/limit=10/genre=16/explicit=true/xml"/><icon>http://phobos.apple.com/favicon.ico</icon><author><name>iTunes Store</name><uri>http://www.apple.com/itunes/</uri></author><rights>Copyright 2008 Apple Inc.</rights>
        <entry>
            <updated>2012-04-01T07:22:41-07:00</updated>
                <id im:id="509605055">http://itunes.apple.com/us/album/eyes-open/id509605019?i=509605055&amp;uo=2</id>
                <title>Eyes Open - Taylor Swift</title>
                <im:name>Eyes Open</im:name>
                <link rel="alternate" type="text/html" href="http://itunes.apple.com/us/album/eyes-open/id509605019?i=509605055&amp;uo=2"/>
                <im:contentType term="Music" label="Music"><im:contentType term="Track" label="Track"/></im:contentType>
                <category term="Soundtrack" scheme="http://itunes.apple.com/us/genre/music-soundtrack/id16?uo=2" label="Soundtrack"/>

                <link title="Preview" rel="enclosure" type="audio/x-m4a" href="http://a2.mzstatic.com/us/r1000/116/Music/88/70/a6/mzi.gcauwkkw.aac.p.m4a" im:assetType="preview"><im:duration>30000</im:duration></link>

                <im:artist href="http://itunes.apple.com/us/artist/taylor-swift/id159260351?uo=2">Taylor Swift</im:artist>
                <im:price amount="1.29000" currency="USD">$1.29</im:price>
                <im:image height="55">http://a3.mzstatic.com/us/r1000/069/Music/v4/15/59/19/15591949-a525-99e8-0c50-45697b0ec78b/UMG_cvrart_00602527969206_01_RGB72_1200x1200_12UMGIM10247.55x55-70.jpg</im:image>
                <im:image height="60">http://a5.mzstatic.com/us/r1000/069/Music/v4/15/59/19/15591949-a525-99e8-0c50-45697b0ec78b/UMG_cvrart_00602527969206_01_RGB72_1200x1200_12UMGIM10247.60x60-50.jpg</im:image>
                <im:image height="170">http://a3.mzstatic.com/us/r1000/069/Music/v4/15/59/19/15591949-a525-99e8-0c50-45697b0ec78b/UMG_cvrart_00602527969206_01_RGB72_1200x1200_12UMGIM10247.170x170-75.jpg</im:image>
                <rights>2012 Universal Republic Records, a division of UMG Recordings, Inc.</rights>
                <im:releaseDate label="March 20, 2012">2012-03-20T00:00:00-07:00</im:releaseDate>
                <im:collection><im:name>The Hunger Games (Songs from District 12 and Beyond)</im:name><link rel="alternate" type="text/html" href="http://itunes.apple.com/us/album/hunger-games-songs-from-district/id509605019?uo=2"/><im:contentType term="Music" label="Music"><im:contentType term="Album" label="Album"/></im:contentType></im:collection>

(etc…)

With the PHP script, I’m able to get results for things like the title, id, im:image for each [entry] to use in the script output. What I need to get is the url from one of the link entries. Specially I need the url from the “Preview” link:

<link title="Preview" rel="enclosure" type="audio/x-m4a" href="http://a2.mzstatic.com/us/r1000/116/Music/88/70/a6/mzi.gcauwkkw.aac.p.m4a" im:assetType="preview"><im:duration>30000</im:duration></link>

In this case, we would need the a2.mzstatic.com/us/r1000/116/Music/88/70/a6/mzi.gcauwkkw.aac.p.m4a link for use in the script results for each of the 10 entries.

How do I capture that href for the .m4a audio file “Preview” link in the above Atom feed?

Here is a portion of the PHP script where we get the contents of the iTunes Atom url, cycle through the 10 results, and generate HTML for each entry via $rssresults that is called in a site template.

    $string = file_get_contents('http://itunes.apple.com/us/rss/topsongs/limit=10/genre=16/explicit=true/xml');

// Remove the colon ":" in the <xxx:yyy> to be <xxxyyy>
$string = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $string);

if ($f = @fopen($cache_file, 'w')) {
    fwrite ($f, $string, strlen($string));
    fclose($f);
    }
}
$xml = simplexml_load_string($string);

// Output
$rssresults = '';
$count = 1;
$max = 11;

foreach ($xml->entry as $val) {
    if ($count < $max) {

    $rssresults .= '
        <a href="'.$val->id.'" title="'.$val->title.'"><img src="'.$val->imimage[2].'" alt="'.$val->title.'"></a>

        // .m4a preview url?
        <div><a href=" ">Preview</div>

        <div><strong>'.$count.'. <a href="'.$val->id.'" title="'.$val->title.'">'.$val->title.'</a></strong></div>
        <div> from '.$val->imcollection->imname.'</div>;
    }
    $count++;
}

Any ideas on how to add the “.m4a preview url” to the above script for each entry?

Appreciate any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:46:30+00:00Added an answer on June 1, 2026 at 6:46 am

    In your foreach loop try $val->link[1]["href"] would give you the URL

    foreach ($xml->entry as $val) {
        // echo the link of Preview
        echo $val->link[1]["href"];
    }
    

    Explanation:
    As there are multiple link entry you can access them by array index. So index 1 is used to access the second link entry. Each attribute of an Element can be accessed by its name as a key to the element. Hence $val->link[1]["href"] would give you http://a2.mzstatic.com/us/r1000/116/Music/88/70/a6/mzi.gcauwkkw.aac.p.m4a

    Viper-7

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to parse this XML file for a school project: http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/genre=20/xml .
I am using NSXMLParser to parse this XML: http://itunes.apple.com/gb/rss/topsongs/limit=50/explicit=true/xml . I am trying to
I am trying to parse iTunes top movies (top songs, albums etc) RSS feed,
Am trying to parse the RSS feed from www.ted.com/talks/rss, I can access all normal
Trying to parse out lat/lon from a google maps rss feed: $file = http://maps.google.com/maps/ms?ie=UTF8&hl=en&vps=1&jsv=327b&msa=0&output=georss&msid=217909142388190116501.000473ca1b7eb5750ebfe;
Iam trying to parse rss feed from particular url... here is my php file
Trying to parse a YouTube feed using PHP simplexml_load_file(); I am able to access
I have an xml feed at this url Now im trying parse the content,
I am trying to parse XML using PHP DOM and then insert this data
Trying to parse Maplines for an airport. Each airport can have X number of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.