When I started building my mashups with PHP one idea I had was to fetch audio previews for songs and albums, this was actually 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. Last.fm 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.
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.
My solution is part of my musicCollection.php class but it is actually 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 the 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.
I think I’ll finally switch my mashups to a CSV file that is a subset of my actual collection of albums as things will run faster and smoother. This code has been tested on 100s of different albums, if an album isn’t in iTunes or there are various different editions of an album, or there two albums by the same artist with the same album title…
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.
- CD Cover Gallery is pretty much done
- CD Cover Gallery PHP Code
- Creating an album cover gallery
- My PHP classes for making web mashups
This entry was originaly posted on , it was last edited on and is filed under: Internet and tagged: Amazon Product Advertising API, API, Apple, CURL, gitHub, iTunes, JSON, Last.fm, mashup, OAuth, PHP, TOS.