Question

I'm trying to retrieve release info from the MusicBrainz database using a PHP script on my server. I have a list of songs, with song title and artist name, and I'm trying to retrieve the date of the first release of that song, along with other info about that release.

I realise that the search won't always be 100% accurate, but the list consists of fairly rare and unique songs so it should at least get me on a good track.

I've gotten pretty far with my script, it returns results and everything, but I'm unsure about how to write the query exactly. The documentation is quite confusing and doesn't feature an example where you search for both song title and artist.

This is my code:

// this info is normally fetched from my DB, but just as a simple example (as it is returned):
$artist = "ZZTop"; 
$song_title = "It's only Love";

// I'm having trouble with this part:
$mb_query = 'http://www.musicbrainz.org/ws/2/recording?query=' . $song_title .' ANDartist:' . $artist ;

$xml = simplexml_load_file($mb_query);

$releasedate = $xml->{'recording-list'}->recording[0]->{'release-list'}->release[0]->date;

At first I've tried to rawurlencode() the $artist and $song_title, but funnily enough, that didn't return any results, so I figured I'd just leave it as a plain string. The query returns results, but they are really off and I have the feeling only part of the query is getting picked up (for instance only the song title and not the artist).

Does anyone know the right way to do this?

Was it helpful?

Solution

The query created by your example code is this: http://musicbrainz.org/ws/2/recording?query=It%27s%20only%20Love%20ANDartist:ZZTop

The problems are:

  • ANDartist: should be AND artist:
  • ZZTop should be "ZZ Top", otherwise the artist isn't found. You can add an alias `ZZTop if you really think that is how many people spell it
  • You might want to use phrases ("...") to search for full titles. Otherwise MB searches for titles including (It's or only or Love) and `artist:"ZZ TOP". However, results containing all words will be rated higher and show up on the top. So this is optional.

So the correct/precise query to use would be: http://musicbrainz.org/ws/2/recording?query=%22It%27s%20only%20Love%22%20AND%20artist:%22ZZ%20Top%22 (2 results)

A more fuzzy query that works would be: http://musicbrainz.org/ws/2/recording?query=It%27s%20only%20Love%20AND%20artist:%28ZZ%20Top%29 (80 results, using artist:(ZZ Top) to search for ZZ or Top artists)

See the MusicBrainz Search Documentation and Lucene Search Syntax for Details.

This code works for me (on PHP 5.5.13) instead of your line:

$mb_query = 'http://www.musicbrainz.org/ws/2/recording?query="'.$song_title.'"'
            .' AND artist:"'.$artist.'"';

The PHP Documentation say you only need to use rawurlencode() prior to PHP 5.1.0.

Additionally you might want to use a pre-made library to work with the MusicBrainz Web Service more easily. There is a PHP Library for WS/2 listed on the MB Documentation. I haven't tried it myself though.


bonus:

If you have problems finding recordings because the artist is spelled differently on your end you can search for the artist (including aliases) first and then use the id of the artist for the recording search. Note that you can't use the alias in a recording search directly.

This query will search for ZZTop in the artist names, artist aliases and artist sortname: http://musicbrainz.org/ws/2/artist?query=%22ZZTop%22 (see the artist search field documentation).

With that search you get an unique ID: a81259a0-a2f5-464b-866e-71220f2739f1. Note that you might get multiple results, so you might want to save a list with results with a high score and try other entries when you can't find the recording in the next step.

Now you can use the ID instead of the name in the recording search: http://musicbrainz.org/ws/2/recording?query=%22I%27ts%20only%20Love%22%20AND%20arid:a81259a0-a2f5-464b-866e-71220f2739f1

You can also use arid:(... OR ...) when you got multiple results for the query for the artist.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top