Spotify tiene una API muy sencilla de usar. Sin Logins, Oauth ni nada. Peticiones por url y datos en formato XML. Muy sencillo todo. Esto es porque solo te permite pedir información, no interactuar.
Hoy he estado jugando un poco con la API. No he conseguido hacer lo que quería -mostrar una lista de musica propia desglosando sus títulos en una web-. Parece que con la api para cuentas no premium no pueden consultarse esos datos...
Aún así, he fabricado una miniclase para consultar uris de Spotify (de albumes, artistas o tracks) y ya que la he hecho la publico aquí por si alguien -o yo mismo dentro de un tiempo- la necesita.
Es muy sencilla de usar y tiene solo un método: spotify::get()
<?php // Se incluye la clase include_once("spotify.class.php"); // Se rescatan los datos de una uri de spotify (de artista, album o track, no de listas propias) $data = spotify::get('spotify:artist:1yxSLGMDHlW21z4YXirZDS'); // Ya tenemos los datos formateados para trabajar. var_dump($data); ?>
Solo un detalle: La api de Spotify solo permite 10 consultas por segundo al mismo servidor. Por ese motivo es recomendable no anidar consultas una tras otra y cachear los resultados -en base de datos, fichero o ram- para que no te bloqueen el acceso durante un rato.
Seguidamente la clase:
<?php class spotify { static $baseurl = "http://ws.spotify.com/lookup/1/?"; static public function get( $spotifyUri ) { // Api en: http://developer.spotify.com/en/metadata-api/lookup/ $data = 'uri='.$spotifyUri; switch(substr($spotifyUri,8,5)) { case 'album': $data .= '&extras=trackdetail'; $method = 'album'; break; case 'artis': $data .= '&extras=albumdetail'; $method = 'artist'; break; case 'track': // nothing extra :( $method = 'track'; break; default: $method = false; } $xml = @simplexml_load_file(self::$baseurl . $data); if ($xml && $method) { return self::$method($xml,$spotifyUri); } else { return false; } } /** * spotify_uri2open * * Función para transformar uris con protocolo spotify en urls http de open spotify. */ public function uri2open($uri) { return ($uri) ? "http://open.spotify.com/" . str_replace(":","/",substr($uri,8)) : false; } protected function album($xml,$spotifyUri) { $element = array( 'type' => 'album', 'url' => self::uri2open($spotifyUri), 'name' => (string) $xml->name, 'year' => (string) $xml->released, 'artist' => (string) $xml->artist->name, 'artist_url' => self::uri2open($xml->artist['href']), ); foreach ($xml->tracks->track as $t) { $element['item'][] = array( "type" => 'track', "url" => self::uri2open($t['href']), "name" => (string) $t->name, 'artist' => (string) $t->artist->name, 'artist_url' => self::uri2open($t->artist['href']), 'length' => (string) $t->length, 'popularity' => (int) $t->popularity ); } return $element; } protected function artist($xml,$spotifyUri) { $element = array( 'type' => 'artist', 'url' => self::uri2open($spotifyUri), 'name' => (string) $xml->name, ); foreach ($xml->albums->album as $a) { $element['item'][] = array( 'type' => 'album', 'url' => self::uri2open($a['href']), 'name' => (string) $a->name, 'year' => (string) $a->released, 'artist' => (string) $a->artist->name, 'artist_url' => self::uri2open($a->artist['href']), ); } return $element; } protected function track($xml,$spotifyUri) { $element = array( "type" => 'track', "url" => self::uri2open($spotifyUri), "name" => (string) $xml->name, 'artist' => (string) $xml->artist->name, 'artist_url' => self::uri2open($xml->artist['href']), 'length' => (string) $xml->length, 'popularity' => (int) $xml->popularity ); return $element; } }
Posts Relacionados:

Si que te la has currado
Thanks por compartirla!
Bueno, es bastante tonta, pero a mi me resulta más cómodo preocuparme de las apis una sola vez y a partir de ahi trabajar solo con arrays así que así se queda!
Gracias por el comentario.