Artist and Song search of Spotify and iTunes via web URL

Posted on Updated on

This took me a while to find out how to do, so I thought I’d share my research to you.

I had a problem where I wanted users to be able to click on a link and it would open up the Spotify, or iTunes program to the search results of the values I passed it.

As mentioned, this took a long while to get. The Spotify one was easy as, and they should be commended for their simplicity. The iTunes one was not as easy to find.

So, this is my resulting code example, which I hope will help you in the future.

See this example work in JSFiddle
https://jsfiddle.net/vr_driver/e4q2nme3/13/

HTML Code

<a href="https://search.itunes.apple.com/WebObjects/MZSearch.woa/wa/advancedSearch?genreIndex=1&media=music&restrict=true&submit=seeAllLockups&entity=songs&allArtistNames=tobyMac&allTitle=Love+Broke+Thru"><i class="fa fa-apple"></i>Apple iTunes</a>
<a href="spotify:search:tobyMac+Love+Broke+Thru"><i class="fa fa-spotify"></i>Spotify</a>

PHP Source Code

$artist = "TobyMac";
$title = "Love Broke Through";
echo '<a href="spotify:search:' . str_replace(" ","+",$artist) . "+" . str_replace(" ","+",$title) . '"><i class="fa fa-spotify"></i> Spotify</a>';
echo '<a href="https://search.itunes.apple.com/WebObjects/MZSearch.woa/wa/advancedSearch?genreIndex=1&media=music&restrict=true&submit=seeAllLockups&entity=songs&allArtistNames=' . str_replace(" ","+",$artist) . "&allTitle=" . str_replace(" ","+",$title) . '"><i class="fa fa-apple"></i> iTunes</a>';

// Or so the browser opens a window and then closes it.
$iTunes_link = "https://search.itunes.apple.com/WebObjects/MZSearch.woa/wa/advancedSearch?genreIndex=1&media=music&restrict=true&submit=seeAllLockups&entity=songs&allArtistNames=". str_replace(" ","+",$artist) . "&allTitle=" . str_replace(" ","+",$title);
echo '<a href="javascript:closeOnLoad(\'' . $iTunes_link. '\');"><i class="fa fa-apple"></i></a>';

JS

<script type="text/Javascript">
function closeOnLoad(myLink)
{
    var newWindow = window.open(myLink);
    newWindow.blur();
    setTimeout
    (
	function()
	{
	  newWindow.close();
	},
	1000
    );
};
</script>

Both of these examples do 2 different things.
The Full iTunes example opens up a new window by JS and then closes it a second later. This is to allow the page to load, and then have the Apple page call the iTunes program. It then opens it to an advanced search page (which until I started playing with this stuff didn’t even know existed).

The Spotify example just opens the app directly if you have it installed – if you don’t use WordPress of course. They’ve wrecked the link in the example by removing the “spotify:search:” at the front of the link.

If this has helped you, please let me know, I’d love to hear, even send me a donation through PayPal.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s