MusicBrainz artist info API

·

The MusicBrainz web service provides lots of information about artists, release, tracks and other entities. Here's how to fetch an artist's details, official album releases, band members and URLs in PHP5:

  $artist_enc = urlencode($artist);
    
  if ($album){
    $album_enc = urlencode($album);
    $xml = simplexml_load_file("http://musicbrainz.org/ws/1/release/?type=xml&limit=1&title={$album_enc}&artist={$artist_enc}");
    $artist_mbid = $xml->{'release-list'}->release[0]->artist['id'];
  }
  
  if (!$artist_mbid){
    $xml = simplexml_load_file("http://musicbrainz.org/ws/1/artist/?type=xml&limit=1&name={$artist_enc}");
    $artist_mbid = $xml->{'artist-list'}->artist[0]['id'];
  }
  
  if ($artist_mbid){
      $xml = simplexml_load_file("http://musicbrainz.org/ws/1/artist/{$artist_mbid}?type=xml&inc=sa-Official+url-rels+artist-rels");
      if ($artist = $xml->artist) {
        $artist->registerXPathNamespace('mb', 'http://musicbrainz.org/ns/mmd-1.0#');    
        $releases = $artist->xpath("mb:release-list/mb:release");  
        $members = $artist->xpath("mb:relation-list[@target-type='Artist']/mb:relation[@type='MemberOfBand'][@direction='backward']");
        $urls = $artist->xpath("mb:relation-list[@target-type='Url']/mb:relation");
      }
  }