Skip to content

Commit 635c44d

Browse files
committed
Update musicbrainz example
1 parent 5bb8097 commit 635c44d

2 files changed

Lines changed: 103 additions & 40 deletions

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
/**
3+
* Do an XML lookup from musicbrainz.org and parse/filter the results.
4+
*
5+
* This example shows how to make a simple REST-style request against a remote
6+
* server.
7+
*
8+
* This does two HTTP requests - one to get information about a band, and another
9+
* to get a list of albums put out by that band.
10+
*
11+
* @author M Butcher <matt@aleph-null.tv>
12+
* @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license.
13+
* @see https://musicbrainz.org
14+
*/
15+
require_once __DIR__ . '/../../vendor/autoload.php';
16+
17+
$artist_name = 'U2';
18+
$artist_url = 'https://musicbrainz.org/ws/2/artist/?query=' . rawurlencode($artist_name);
19+
$album_url = 'https://musicbrainz.org/ws/2/release-group?&artist=';
20+
21+
try {
22+
/* Make a remote cURL request to get the XML */
23+
$artist_xml = get($artist_url);
24+
25+
/* Load the XML into QueryPath and select the first artist in the list */
26+
$artist = qp($artist_xml, 'artist:first');
27+
28+
/* Check if nothing in the XML matched the selector */
29+
if (count($artist) === 0) {
30+
echo '<h1>No results found</h1>';
31+
exit;
32+
}
33+
34+
/* Get direct children of "artist", filter by the "name" tag, and output the text */
35+
echo sprintf('<h1>Albums by <em>%s</em></h1>', $artist->children('name')->text());
36+
37+
/* Get the unique albums listed for this artist */
38+
$id = $artist->attr('id');
39+
$album_url .= rawurlencode($id);
40+
$albums_xml = get($album_url);
41+
42+
/* Load the XML into QueryPath */
43+
$albums = qp($albums_xml, 'release-group');
44+
45+
/* Loop over the results */
46+
echo '<ol>';
47+
foreach ($albums as $album) {
48+
echo sprintf(
49+
'<li>%1$s (%2$s)</li>',
50+
$album->find('title')->text(),
51+
$album->find('first-release-date')->text()
52+
);
53+
}
54+
echo '</ol>';
55+
56+
/* The XML retrieved via cURL and fed into QueryPath */
57+
echo '<h2>XML</h2>';
58+
59+
echo '<h3>Artists</h3>';
60+
echo sprintf('<code>%s</code>', $artist_url);
61+
echo '<pre><code>';
62+
echo htmlspecialchars($artist->top()->xml());
63+
echo '</code></pre>';
64+
65+
echo '<h3>Albums</h3>';
66+
echo sprintf('<code>%s</code>', $album_url);
67+
echo '<pre><code>';
68+
echo htmlspecialchars($albums->top()->xml());
69+
echo '</code></pre>';
70+
71+
} catch (Exception $e) {
72+
echo $e->getMessage();
73+
}
74+
75+
/**
76+
* Make a GET request using cURL and return the results
77+
*
78+
* @param string $url
79+
* @return string
80+
*/
81+
function get($url)
82+
{
83+
$defaults = array(
84+
CURLOPT_URL => $url,
85+
CURLOPT_HEADER => 0,
86+
CURLOPT_RETURNTRANSFER => true,
87+
CURLOPT_FAILONERROR => true,
88+
CURLOPT_FOLLOWLOCATION => true,
89+
CURLOPT_USERAGENT => 'QueryPath/3.0'
90+
);
91+
92+
$ch = curl_init();
93+
94+
curl_setopt_array($ch, $defaults);
95+
96+
if (!$result = curl_exec($ch)) {
97+
throw new RuntimeException(curl_error($ch));
98+
}
99+
100+
curl_close($ch);
101+
102+
return $result;
103+
}

examples/musicbrainz.php

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)