Muschamp Rd

Finding audio previews for album tracks in iTunes with PHP

March 3rd, 2013
PHP logo

When I started building my mashups with PHP one idea I had was to fetch audio previews for songs and albums, this was supposedly possible with the Amazon Product Advertising API, but I could never get it to work with the limited data I had in my csv file. does not provide audio previews through their API either. iTunes Music Store isn’t perhaps known for their API, nor can you earn any referral income if you’re Canadian but I actually was able to fetch direct links to audio previews from the iTunes Music Store using their Search API.

Update February 9th 2014: This technique may not work anymore, there appear to still be preview links in Apple’s documentation but I’ve noticed a distinct lack of successfully finding tracks in my mashups which have been running for years, maybe I’m being blocked, but the API is returning data, I don’t have a lot of time to debug old code at the moment. Maybe I no longer count as being active.

I’m not sure Apple would applaud this, I’ve been doing it for well over a year, but it is only used as part of one or two of my web mashups. I don’t think I violate the TOS, I do cache the returned JSON results but only to prevent repeated redundant calls to Apple’s API. I don’t cache images and certainly not audio previews. I’m not sure I meet their display requirements, frickin’ display requirements…

Considering how much time and effort I spent debugging and re-reading API documentation there may be other methods I’ve written which people would find useful, but no one uses because they don’t want to jump through all the hoops necessary to use my mashup codebase. Apple doesn’t require public and private keys or OAuth or hashing, you can just format a URL and get back data. It is up to you to format the URL correctly and parse the data, this is non-trivial.

This code is part of my musicCollection.php class but it is actually only three methods, one which makes requests to the other two. It is necessary to make multiple calls to Apple’s Search API in order to get the resulting preview tracks for an entire album. You can rip out the caching if you like. My caching technique isn’t terribly sophisticated, it is contained in caching.php and the code below may need one function from muskLib.php which is just a wrapper for cURL.  All this code is in GitHub.

There is no warranty or guarantee for this code and you are responsible for not breaking Apple’s Terms of Service and/or getting throttled for making excessive number of requests, I recommend you cache what you can.

Album track previews from iTunes working on my random CD mashup

Related Posts

One Comment

  • Muskie says:

    This code is actually quite useful, someone could make money with it, but not me as I’m Canadian… It appears there may be a bug in my damn Twitter code, it was working perfectly when I posted it to my blog the other day, but now I’m not so sure.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Posts on Muskblog © Andrew "Muskie" McKay.
CFA Institute does not endorse, promote or warrant the accuracy or quality of Muskblog. CFA® and Chartered Financial Analyst® are registered trademarks owned by CFA Institute.