Because I'm rather kind and all, I've decided to release LastLib, a music data fetcher that I've decided to tweak up. It was originally going to be used with me, Tom and Ryan's project called DiskDock, but that fell over so I might as well release this for free. Feel free to use it however you want
It should be pretty self explanatory. It fetches details of artists and albums from the social music website Last.fm. All the data is very accurated, and it is a big database with pretty much everything. All you need to do is call one of the functions, and it'll do it's thanggg! Enjoy!
PHP Code:<?php
# LastLib
# by Tim Rogers
# with thanks to Ryan, Tom and Audioscrobbler
# library.inc.php
# REQUIRES SIMPLEXML (INLCUDED in PHP5)
function createXmlObject($url) {
error_reporting(0);
$xml = file_get_contents($url) or die("<span style='color: red; font-family: Verdana; font-size: 10px;'><b>ERROR:</b> Please try again later. Artist or album may be incorrect.</span>"); /* Get the contents of the XML file */
error_reporting(E_ALL);
$simplexml = new SimpleXMLElement($xml); /* Create a new SimpleXML object with the XML file */
return $simplexml; /* Return the object from the function */
}
function getAlbumArt($artist, $album) {
$artist = str_replace(" ", "%20", $artist); /* Replace spaces in the artist with URL encoded spaces (%20) */
$album = str_replace(" ", "%20", $album); /* Replace spaces in the album with URL encoded spaces (%20) */
$url = "http://ws.audioscrobbler.com/1.0/album/" . $artist . "/" . $album . "/info.xml"; /* Form the URL of the XML file */
$feed = createXmlObject($url); /* Create a SimpleXML object with the URL */
$arturl = $feed->coverart->large; /* Fetch the album art URL using SimpleXML into $arturl */
return $arturl; /* Return to URL of the album art */
}
function getReleaseDate($artist, $album) {
$artist = str_replace(" ", "%20", $artist); /* Replace spaces in the artist with URL encoded spaces (%20) */
$album = str_replace(" ", "%20", $album); /* Replace spaces in the album with URL encoded spaces (%20) */
$url = "http://ws.audioscrobbler.com/1.0/album/" . $artist . "/" . $album . "/info.xml"; /* Form the URL of the XML file */
$feed = createXmlObject($url); /* Create a SimpleXML object with the URL */
$datexml = $feed->releasedate; /* Fetch the release date and time using SimpleXML into $datexml */
$date = explode(", ", $datexml); /* Split the release date into time and date */
$datex = $date[0]; /* Only keep the first part, which is the date */
return $datex; /* Return the release date without the time */
}
function tracksArray($artist, $album) {
$artist = str_replace(" ", "%20", $artist); /* Replace spaces in the artist with URL encoded spaces (%20) */
$album = str_replace(" ", "%20", $album); /* Replace spaces in the album with URL encoded spaces (%20) */
$url = "http://ws.audioscrobbler.com/1.0/album/" . $artist . "/" . $album . "/info.xml"; /* Form the URL of the XML file */
$feed = createXmlObject($url); /* Create a SimpleXML object with the URL */
$tracks = array(); /* Create an array in $tracks */
foreach ($feed->tracks->track as $thetrack) { /* Foreach track in the XML as a variable */
$count = count($tracks); /* Count the number of tracks added to the array so far */
$tracks[$count] = $thetrack['title']; /* Get the title of the track and use the number counted earlier as the index in the array */
}
return $tracks; /* Return the tracks array to be handled */
}
function similarArray($artist) {
$artist = str_replace(" ", "%20", $artist); /* Replace spaces in the artist with URL encoded spaces (%20) */
$url = "http://ws.audioscrobbler.com/1.0/artist/" . $artist . "/similar.xml"; /* Form the URL of the XML file */
$feed = createXmlObject($url); /* Create a SimpleXML object with the URL */
$similar = array(); /* Create an array in $similar */
foreach ($feed->artist as $anartist) { /* Foreach similar artist in XML as a variable */
$count = count($similar); /* Count the number of similar artists added to the array so far */
$similar[$count] = $anartist->name; /* Get the name of the similar artist and use the number counted earlier as the index in the array */
}
return $similar; /* Return the similar tracks array to be handled */
}
?>









Reply With Quote