{"id":97,"date":"2010-01-21T13:33:23","date_gmt":"2010-01-21T13:33:23","guid":{"rendered":"http:\/\/barrycarlyon.co.uk\/wordpress\/?p=97"},"modified":"2010-01-25T00:43:13","modified_gmt":"2010-01-25T00:43:13","slug":"curl-musicbrainz-and-php","status":"publish","type":"post","link":"https:\/\/barrycarlyon.co.uk\/wordpress\/2010\/01\/21\/curl-musicbrainz-and-php\/","title":{"rendered":"Curl, MusicBrainz and PHP"},"content":{"rendered":"<p>As part of the New Years Resolutions I didn&#8217;t make.<\/p>\n<p>I&#8217;m updating my blog with some Code Snippets!<\/p>\n<p>Been fiddling about with my PHP powered Jukebox that plays out on <a href=\"http:\/\/LSRfm.com\/\">LSRfm.com<\/a> (<a href=\"http:\/\/LSRfm.com\/\">Leeds Student Radio<\/a>) Overnight, and there is a need for a bulk track adder.<\/p>\n<p>So, I&#8217;ve been tidying up the <a href=\"http:\/\/musicbrainz.org\/\">MusicBrainz<\/a> Data Fetcher, as well as fiddling with PHP and getting ID3 tags from Files. But thats a different blog post.<\/p>\n<p>Essentially there is a (new-ish non updated recently) PHP Library for the interaction with MusicBrainz, (A music database), recently found that (this morning its rubbish), but of no good.<\/p>\n<p>So went back to my curl method.<\/p>\n<p>Tidyied it up and got it down to a few less lines.<\/p>\n<p>Essentially for a given track, the PHP extracts the ID3 tags, and then passes it to this function: (its needs making into a function btw :-P)<\/p>\n<blockquote>\n<pre>$curl = new curl();\r\n\r\n$data = array(\r\n\t'title'\t\t=> 'Showdown',\r\n\t'artist'\t=> 'Pendulum',\r\n\t'release'\t=> 'In Silico',\r\n\t'duration'\t=> 327784,\r\n\/\/\t'tracknumber'\t=> 0,\r\n\/\/\t'count'\t\t=> 10,\r\n\/\/\t'releasetype'\t=> '',\r\n\r\n\t'limit'\t\t=> 25,\r\n\t'limit'\t\t=> 1,\r\n);\r\n$target = 'http:\/\/musicbrainz.org\/ws\/1\/track\/?type=xml';\r\nforeach ($data as $ref => $dat) {\r\n\t$target .= '&' . $ref . '=' . urlencode($dat);\r\n}\r\n\r\n                                $curl->target($target);\r\n                                $curl->runit();\r\n                                $mb = $curl->bodyarray['metadata'];\r\n\r\nif (isset($mb['track-list'])) {\r\n\t$mb = $mb['track-list'];\r\n\r\n\tif (isset($mb['track']['0'])) {\r\n\t\t$mb = $mb['track'];\r\n\t\t$artist_id = $mb['0']['artist_attr']['id'];\r\n\t\t$song_id = $mb['0_attr']['id'];\r\n\t} else {\r\n\t\t$artist_id = $mb['track']['artist_attr']['id'];\r\n\t\t$song_id = $mb['track_attr']['id'];\r\n\t}\r\n} else {\r\n\t\/\/ no data\r\n}\r\n\r\necho \"\\n\" . $artist_id . ' ' . $song_id;<\/pre>\n<\/blockquote>\n<p>Where new curl() just calls my Curl Class.<\/p>\n<p>I&#8217;m sure you have your own ways of doing curl, but mine just sets the target with $curl-&gt;target and curlexec() with $curl-&gt;runit.<\/p>\n<p>The result is in $curl-&gt;body, or exploded nicely as an array in $curl-&gt;bodyarray. I&#8217;ll post about my curl later! As well as some other Carlyon_CMS stuff.<\/p>\n<p>So thats a quick rough and ready way to get a artist and song ID from MusicBraiz.<\/p>\n<p>Its worth noting that since my limit is st to 1.<\/p>\n<p>The first option of if (isset($mb[&#8216;track&#8217;][&#8216;0&#8217;]) is entirely redundant, as that only triggers when mb returns more than 1 result.<\/p>\n<p>(I only just added limit before writing this post, which is where I decided to write this post)<\/p>\n<p>The MusicBrainz Docs for its XML service is at: <a href=\"http:\/\/musicbrainz.org\/doc\/XML_Web_Service\">http:\/\/musicbrainz.org\/doc\/XML_Web_Service<\/a> If your Interested!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A quick block of PHP to fetch MusicBrainz ID for a song and artist based on Song Data.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[81,88,45],"tags":[331,82,86,87,85,84,83],"class_list":["post-97","post","type-post","status-publish","format-standard","hentry","category-carlyon_cms","category-code-geekery","category-geekery","tag-carlyon_cms","tag-code","tag-curl","tag-music","tag-musicbrainz","tag-php","tag-snippet"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/barrycarlyon.co.uk\/wordpress\/wp-json\/wp\/v2\/posts\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barrycarlyon.co.uk\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barrycarlyon.co.uk\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barrycarlyon.co.uk\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barrycarlyon.co.uk\/wordpress\/wp-json\/wp\/v2\/comments?post=97"}],"version-history":[{"count":0,"href":"https:\/\/barrycarlyon.co.uk\/wordpress\/wp-json\/wp\/v2\/posts\/97\/revisions"}],"wp:attachment":[{"href":"https:\/\/barrycarlyon.co.uk\/wordpress\/wp-json\/wp\/v2\/media?parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barrycarlyon.co.uk\/wordpress\/wp-json\/wp\/v2\/categories?post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barrycarlyon.co.uk\/wordpress\/wp-json\/wp\/v2\/tags?post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}