Have you ever wanted to link to an iTunes page? It’s easy enough to copy a long, nasty-looking URL like http://itunes.apple.com/us/album/yours-truly/id310905907
right from the application by right-clicking on the album art or title. This works well enough if you’re manually linking to an album or iPhone app from a blog post, but what if you need something a bit more user friendly?
It’s not documented terribly well, but you can create much nicer short “search” links using the itunes.com
domain. Here are a few examples:
You can even combine the search links with your iTunes affiliate ID, if you know how to do it right. It looks something like this:
http://itunes.com/artist/album?partnerId=30&siteID=YOUR_AFFILIATE_ID
Now for the fun part… Wouldn’t it be neat to be able to generate the links automatically? I’m doing that over at Folk Music Site (a fun project I put together last summer, as I thought it would be neat to have a directory of noteworthy folk musicians). Here is some simple PHP that will generate iTunes links on-the-fly:
function get_itunes_link($artist, $album) {
$affiliate_id = ''; //Linkshare affiliate ID for iTunes
$artist = clean_itunes_string($artist);
$album = clean_itunes_string($album);
$link = "http://itunes.com/{$artist}/{$album}?partnerId=30&siteID={$affiliate_id}";
return $link;
}
function clean_itunes_string($string) {
$string = strtolower(str_replace(' ', '', $string)); //remove whitespace and makes everything lower-case
$string = str_replace('&', 'and', $string); //Replace ampersands with the word 'and'
$remove = array('!', '¡', '"', '#', '$', '%', '\'', '(', ')', '*', '+', ',', '\\', '-', '.', '/', ':', ';', '<', '=', '>', '¿', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~', '©', '®', '™');
$string = str_replace($remove, '', $string); //Remove special characters that iTunes doesn't like
return $string;
}
echo get_itunes_link('Feist', 'The Reminder');
Note: iTunes links should not have accented characters like á or ü. They should be replaced with their base ASCII counterparts (“a” and “u” respectively). The above code does nothing to sanitize these characters, so you might run into problems unless you write some additional logic to handle that. If you’re using a framework like Kohana, you might have a convenient helper function like utf8::transliterate_to_ascii().