function xml2array($url, $get_attributes = 1, $priority = 'tag') { $contents = ""; if (!function_exists('xml_parser_create')) { return array (); } $parser = xml_parser_create(''); if (!($fp = @ fopen($url, 'rb'))) { return array (); } while (!feof($fp)) { $contents .= fread($fp, 8192); } fclose($fp); xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); xml_parse_into_struct($parser, trim($contents), $xml_values); xml_parser_free($parser); if (!$xml_values) return; //Hmm... $xml_array = array (); $parents = array (); $opened_tags = array (); $arr = array (); $current = & $xml_array; $repeated_tag_index = array (); foreach ($xml_values as $data) { unset ($attributes, $value); extract($data); $result = array (); $attributes_data = array (); if (isset ($value)) { if ($priority == 'tag') $result = $value; else $result['value'] = $value; } if (isset ($attributes) and $get_attributes) { foreach ($attributes as $attr => $val) { if ($priority == 'tag') $attributes_data[$attr] = $val; else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr' } } if ($type == "open") { $parent[$level -1] = & $current; if (!is_array($current) or (!in_array($tag, array_keys($current)))) { $current[$tag] = $result; if ($attributes_data) $current[$tag . '_attr'] = $attributes_data; $repeated_tag_index[$tag . '_' . $level] = 1; $current = & $current[$tag]; } else { if (isset ($current[$tag][0])) { $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; $repeated_tag_index[$tag . '_' . $level]++; } else { $current[$tag] = array ( $current[$tag], $result ); $repeated_tag_index[$tag . '_' . $level] = 2; if (isset ($current[$tag . '_attr'])) { $current[$tag]['0_attr'] = $current[$tag . '_attr']; unset ($current[$tag . '_attr']); } } $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1; $current = & $current[$tag][$last_item_index]; } } elseif ($type == "complete") { if (!isset ($current[$tag])) { $current[$tag] = $result; $repeated_tag_index[$tag . '_' . $level] = 1; if ($priority == 'tag' and $attributes_data) $current[$tag . '_attr'] = $attributes_data; } else { if (isset ($current[$tag][0]) and is_array($current[$tag])) { $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; if ($priority == 'tag' and $get_attributes and $attributes_data) { $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; } $repeated_tag_index[$tag . '_' . $level]++; } else { $current[$tag] = array ( $current[$tag], $result ); $repeated_tag_index[$tag . '_' . $level] = 1; if ($priority == 'tag' and $get_attributes) { if (isset ($current[$tag . '_attr'])) { $current[$tag]['0_attr'] = $current[$tag . '_attr']; unset ($current[$tag . '_attr']); } if ($attributes_data) { $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; } } $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken } } } elseif ($type == 'close') { $current = & $parent[$level -1]; } } return ($xml_array); } // The meaty bit. Kinda. // This part replaces the options within // the [] (eg [name]) with the actual value from the last.fm data. function str_tz($format,$time,$offset){ $time = str_replace(',','',$time); $stamp = strtotime($time); $result = $stamp+(3600*$offset); $result = date($format,$result); return $result; } function lastfmdata($method,$user,$key,$options,$number = 50,$art = '',$offset = '0'){ $error = array(); if(empty($number)){$number = 50;} if(empty($method)){$error[] = 'You left the method blank';} if(empty($key) or $key == 'API_KEY'){$error[] = 'You left the API key blank. Get one from http://www.last.fm/api/account';} if(empty($user)){$error[] = 'You left the username blank.';} if(empty($options)){$error[] = 'You left the formatting blank.';} if(empty($art)){$smallart = 'http://cdn.last.fm/flatness/catalogue/noimage/2/default_album_small.png';}else{$smallart = $art;} if(empty($art)){$mediumart = 'http://cdn.last.fm/flatness/catalogue/noimage/2/default_album_medium.png';}else{$mediumart = $art;} if(empty($art)){$largeart = 'http://cdn.last.fm/flatness/catalogue/noimage/2/default_album_mega.png';}else{$largeart = $art;} $data = xml2array("http://ws.audioscrobbler.com/2.0/?method=user.get$method&user=$user&api_key=$key&limit=$number",0); if($method == 'RecentTracks'){$loop = $data['lfm']['recenttracks']['track'];} if($method == 'TopArtists'){$loop = $data['lfm']['topartists']['artist'];} if($method == 'TopTracks'){$loop = $data['lfm']['toptracks']['track'];} if($method == 'TopAlbums'){$loop = $data['lfm']['topalbums']['album'];} if($method == 'WeeklyTrackChart'){$loop = $data['lfm']['weeklyalbumchart']['album'];} if($method == 'WeeklyAlbumChart'){$loop = $data['lfm']['weeklyalbumchart']['album'];} if($method == 'WeeklyArtistChart'){$loop = $data['lfm']['weeklyartistchart']['artist'];} if($method == 'WeeklyTrackChart'){$loop = $data['lfm']['weeklytrackchart']['track'];} if($method == 'Shouts'){$loop = $data['lfm']['shouts']['shout'];} if($method == 'LovedTracks'){$loop = $data['lfm']['lovedtracks']['track'];} if(!isset($loop['0'])){$looptemp = $loop;unset($loop);$loop[0] = $looptemp;} if($method != '' and empty($error) and !empty($data) and isset($loop[0])){ foreach($loop as $section){ if($count < $number){ $count++; $result = str_replace('[count]',$count,$options); if(is_array($section['artist'])){ $result = str_replace('[artist]',$section['artist']['name'],$result); $result = str_replace('[artisturl]',$section['artist']['url'],$result); }else{ $result = str_replace('[artist]',$section['artist'],$result); } $result = str_replace('[name]',$section['name'],$result); $result = str_replace('[mbid]',$section['mbid'],$result); $result = str_replace('[playcount]',$section['playcount'],$result); $result = str_replace('[album]',$section['album'],$result); $result = str_replace('[url]',$section['url'],$result); if(isset($section['image']['0'])){$smallimg = $section['image']['0'];} if(isset($section['image']['1'])){$mediumimg = $section['image']['1'];} if(isset($section['image']['2'])){$largeimg = $section['image']['2'];} if(empty($smallimg) or preg_match('/noimage/',$smallimg)){ $smallimg = $smallart; } if(empty($mediumimg) or preg_match('/noimage/',$mediumimg)){ $mediumimg = $mediumart; } if(empty($largeimg) or preg_match('/noimage/',$largeimg)){ $largeimg = $largeart; } $result = str_replace('[small]',$smallimg,$result); $result = str_replace('[medium]',$mediumimg,$result); $result = str_replace('[large]',$largeimg,$result); preg_match_all("/\[datefmt\](.*?)\[\/datefmt\]/",$result,$outcome); $datefmt = str_tz($outcome[1][0],$section['date'],$offset); $result = str_replace($outcome[0][0],$datefmt,$result); $date = str_tz('jS M Y, H:i',$section['date'],$offset); $result = str_replace('[date]',$date,$result); $result = str_replace('[author]',$section['author'],$result); $result = str_replace('[body]',$section['body'],$result); echo "$result\n"; } } } elseif(empty($error)){ $error[] = 'Check that there is data to display from your last.fm account, and your API key is correct.'; } if(!empty($error)){ echo 'Last.fm Simple API error:
'; foreach($error as $er){ echo "• $er
"; } } } function lastfmoutput() { // The last.fm username you want to get data for. $username = 'username'; // Your Last.fm API Key. $apikey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // This tells the script what method to use to get the data from last.fm. // Different methods and examples are documented under "methods" below. $method = 'RecentTracks'; // Custom Formatting. In the example below, [url] is converted into the track // URL on last.fm, [name] into the track name and [artist] into the track's artist. // Refer to the documentation for that method below for formatting options. $formatting = '
[artist] - [name]
[artist]
[name]
'; // Number of recent tracks you want to show. (Leave blank for maximum) $number = '1'; // URL for image if no album/track art. (Leave blank for last.fm default) $defaultart = 'http://cdn.last.fm/flatness/catalogue/noimage/2/default_album_mega.png'; // GMT hour offset. (0 or blank means keep the time at GMT, -5 makes the time EST etc.) $offset = '0'; lastfmdata($method,$username,$apikey,$formatting,$number,$defaultart,$offset); }