Patrick Troust Patrick Troust - 1 month ago 9
Javascript Question

How to get all images for an artist with Last.fm API (Need exemples please) PHP

$returnedInfo = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&api_key=" . self::API_KEY . "&artist=" . $artist . "&album=" . $album . "&image=" . self::$size_map[$size] . "&format=json";

Answer

$htmlContent = file_get_contents('http://www.last.fm/fr/music/'.$artist.'/+images');

// We'll add all the images in this array
$images = array();

// Instantiate a new object of class DOMDocument
$doc = new DOMDocument();

// Load the HTML doc into the object
libxml_use_internal_errors(true);
$doc->loadHTML($htmlContent);
libxml_use_internal_errors(false);

// Get all the IMG tags in the document
$elements = $doc->getElementsByTagName('img');

// If we get at least one result
if($elements->length > 0)
{
   // Loop on all of the IMG tags
   foreach($elements as $element)
   {
      // Get the attribute SRC of the IMG tag (this is the link of the image)
      $src = $element->getAttribute('src');

      if (strlen($src) > 0) {
        // Add the link to the array containing all the links
        array_push($images, $src);
      }

   }