Building iTunes Links

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().

  • http://news.runtowin.com Blaine Moore

    I didn’t realize that iTunes had an affiliate program,thanks for the link! That’ll come in handy in the near future.

  • http://www.hzlzh.com hzlzh

    i just want to link a mp3 file on my blog with the link that can be added to one’s itunes when they click it !

  • http://halotis.com Matt

    that’s great, didn’t know you could add the affiliate link to that one.

    I’m looking for something that will work similarly ( links to the app store when on the phone, and computer ) except that if shared on Facebook will find the screenshot of the app instead of saying “Connecting to iTunes”. any ideas?

  • http://www.therock.fm Shane

    I would like to have a ‘buy now’ tab next to our recently played songs on our website, that links to the correct iTunes song. Anyone know how to go about doing this? I’m already an affiliate and am setup with iTunes and Linkshare.

  • Vance Three

    In DC Drywall Contractors, if we want to create a more user-friendly link to an iTunes page, we juts use Apple’s Link Maker tool.

  • Robinson Richards

    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?
    Boundbrook Oaks Estates home contractor