Creating a Spotify playlist with PHP
There's not much info on this one, other than "use spotify-api-server" or "learn objective c and use the coca wrapper" (which I didn't have time to do).
We saw yet another Australian Facebook campaign using Spotify (this time it was Jagermeister) - and the question was proposed "why can't we?" (and so I thought, why not?). I did a little digging, and even though Spotify had their main api (libspotify) in C, the Jagermeister campaign was hitting a PHP page for a response. Apart from the fact it was done by F# (which means they're well versed in Spotify/Facebook/probably have a bunch of programmers etc.) there really was not a lot of information about how to replicate Jagermeisters functionality through PHP. So I gave it a shot, and this is what I've come up with.
Because the link to the campaign will probably be down in the future, below is a really rash explanation of what the Jagermeister Facebook campaign achieved:Â
So basically what it came down to was a few simple steps:
Choose a 'playlist type'
Search for a track
Compile that track with others into a Spotify playlist
Share the Spotify playlist with the user
To do this we need two different APIs, though it could have been done with only libsharp had we been able to do this in C.
Metatune by mikaelbr (github) This is basically a php wrapper to search/lookup Spotify tracks, albums and artists. Yes, this could have been done with Spotify's HTTP request service, but then you need to figure out how to cache the response (as Spotify are big on caching) and I didn't have the time nor the resources to do so (I'm doing this on a Mac, whereas our servers are Linux boxes).
spotify-api-server by liesen (github) This is our saviour. Essentially another PHP wrapper (which doesn't allow song lookup etc.), liesen has pretty much given us the key to Spotify with this one. spotify-api-server allows us to create, modify and lookup details about playlists.
Install
Installing metatune is really easy, it's drag and drop. Installing spotify-api-server gave me a few headaches, though it was my inexperience which let me down. Use homebrew to install the required libraries (make sure you pull the most recent version of homebrews branch first!), something like this:
brew install libevent jannson libspotify
You'll run into a few warnings, but they're harmless. Then start your spotify api server, which will be running through port 1337. We have our metatune running on MAMP through port 8888 (the default) so we can cURL to localhost:1337/ to hit the api server.
Here is my code for metatune which also hits spotify-api-server. It should be drag and drop (into localhost:8888 directory) - the only thing you'll need to modify is line 27 in script.js:
http://open.spotify.com/user/{YOUR USERNAME}/playlist/
DOWNLOAD
Please note: this is not, in any way, shippable code. the css is quite average (even mikaelbr admits this), the js moreso, and the php is probably the worst you've seen. It's a proof of concept, to try and help you out. I wouldn't really take this project and modify it for a product. Start again, with this as a guide. I'll clean it up one day.
A whole bunch of this is just work by mikaelbr/liesen, and just mashed together by me. It's pretty much all their work, and if you can, you should build on their repos. Thanks guys.














