'London', 'from' => '2014-01-01', 'to' => '2014-12-31', 'address' => 'Magnus', 'length' => 'peal' ); # Use the PHP curl library to fetch the data. (Your system may have a # separate package for the PHP curl library that needs installing in # addition to the main PHP library. On Debian it is called php5-curl.) # # BellBoard API documentation: https://bb.ringingworld.co.uk/help/api.php # The export.php URL tells BellBoard to include details of the performances # instead of just a link which is what search.php provides. BellBoard # uses HTTP content negotiation to determine whether to send HTML or XML, # hence the 'Accept: application/xml' header. The RETURNTRANSFER flag # tells curl to return the content from the curl_exec() function rather # than printing it out. $ch = curl_init("https://bb.ringingworld.co.uk/export.php?" . http_build_query($params)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/xml")); $data = curl_exec($ch); curl_close($ch); echo "XML received from BellBoard\n"; echo "---------------------------\n"; echo $data; echo "---------------------------\n"; # PHP has various XML parsing libraries. Personally I like the DOM library # http://php.net/manual/en/book.dom.php but it is can be a bit verbose. # As with curl, you may need to install a separate package to get the PHP # DOM library. $doc = new DOMDocument(); if (!$doc->loadXML($data)) die("Unable to parse XML"); # XPath is a language for identifying parts of an XML document. # http://www.w3.org/TR/xpath/#location-paths gives some example XPath # expressions. As the the BellBoard XML format uses XML namespaces, we # need to register the namespace prefix. We associate it with the 'bb' # prefix, so to refer to a element, we write 'bb:performance'. $xpath = new DOMXPath($doc); $xpath->registerNamespace('bb', 'http://bb.ringingworld.co.uk/NS/performances#'); $perfs = $xpath->query('bb:performance'); echo "Found $perfs->length performances\n\n"; # Extract the date and method from each performance. We do this with an # XPath query with $p, the performance, as the context node (i.e. root). # The DOMXPath inferface returns a list of matching elements even though # we know only one element will be found. foreach ($perfs as $p) { $date = $xpath->query('bb:date', $p)->item(0)->nodeValue; $meth = $xpath->query('bb:title/bb:method', $p)->item(0)->nodeValue; echo "$date\t$meth\n"; }