Created
March 10, 2014 23:04
-
-
Save efoken/9476300 to your computer and use it in GitHub Desktop.
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 | |
define('LASTFM_API_KEY', 'xxx'); | |
function callApi($url, $data = false) | |
{ | |
$curl = curl_init(); | |
if ($data) { | |
$url = sprintf('%s?%s', $url, http_build_query($data)); | |
} | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
return curl_exec($curl); | |
} | |
$response = json_decode(callApi('http://ws.audioscrobbler.com/2.0/', array( | |
'method' => 'event.getinfo', | |
'event' => '3600736', | |
'api_key' => LASTFM_API_KEY, | |
'format' => 'json' | |
))); | |
$artists = (array) $response->event->artists->artist; | |
natcasesort($artists); | |
foreach ($artists as $artist) { | |
$firstLetter = strtolower(substr($artist, 0, 1)); | |
if (!isset($lastFirstLetter) || $lastFirstLetter !== $firstLetter) { | |
echo strtoupper($firstLetter) . "\n"; | |
$lastFirstLetter = $firstLetter; | |
} | |
$response = json_decode(callApi('http://ws.audioscrobbler.com/2.0/', array( | |
'method' => 'artist.getinfo', | |
'artist' => $artist, | |
'api_key' => LASTFM_API_KEY, | |
'format' => 'json' | |
))); | |
$tags = is_object($response->artist->tags) ? (array) $response->artist->tags->tag : array(); | |
if (array_key_exists('name', $tags)) { | |
$tags = array(ucwords($tags['name'])); | |
} else { | |
array_walk($tags, create_function('&$v', '$v = ucwords($v->name);')); | |
} | |
echo utf8_decode($artist) . ' [color=#004080]' . implode('/', $tags) . '[/color]'; | |
echo "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment