Last active
August 29, 2015 14:21
-
-
Save baquiax/99b1c4626c2ae135895f to your computer and use it in GitHub Desktop.
Spotify test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Se incluye la clase | |
include_once("spotify.class.php"); | |
// Se rescatan los datos de una uri de spotify | |
$data = spotify::get('spotify:artist:1yxSLGMDHlW21z4YXirZDS'); | |
echo json_encode($data, JSON_UNESCAPED_SLASHES); | |
//var_dump($data); | |
?> | |
<?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; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment