diff --git a/application/controllers/api.php b/application/controllers/api.php index 5a2ccc71..c6ebda1c 100644 --- a/application/controllers/api.php +++ b/application/controllers/api.php @@ -148,6 +148,7 @@ class API extends CI_Controller { // Retrieve the arguments from the query string $arguments = $this->_retrieve(); + $data['data']['format'] = $arguments['format']; // Call the parser within the API model to build the query $query = $this->api_model->select_parse($arguments); @@ -244,6 +245,7 @@ class API extends CI_Controller { $limit = preg_grep("/^limit\[(.*)\]$/", $this->uri->segments); $order = preg_grep("/^order\[(.*)\]$/", $this->uri->segments); $fields = preg_grep("/^fields\[(.*)\]$/", $this->uri->segments); + $format = preg_grep("/^format\[(.*)\]$/", $this->uri->segments); // Strip each argument $arguments['query'] = substr(array_pop($query), 6); @@ -254,6 +256,8 @@ class API extends CI_Controller { $arguments['order'] = substr($arguments['order'], 0, strlen($arguments['order']) - 1); $arguments['fields'] = substr(array_pop($fields), 7); $arguments['fields'] = substr($arguments['fields'], 0, strlen($arguments['fields']) - 1); + $arguments['format'] = substr(array_pop($format), 7); + $arguments['format'] = substr($arguments['format'], 0, strlen($arguments['format']) - 1); // Return the arguments return $arguments; diff --git a/application/views/api/help.php b/application/views/api/help.php index f48fd690..eb4335db 100644 --- a/application/views/api/help.php +++ b/application/views/api/help.php @@ -81,15 +81,15 @@
  • Key with Read & Write Access
  • Key with Read Only Access
  • -There are a number of API calls you can make from other applications. +There are a number of API calls you can make from other applications, with output available in either XML or JSON.

    API Guide

    Description

    -Query the logbook +Query the logbook, and output in XML format.

    Syntax

    -
  • /search/query[<field><=|~><value>{(and|or)...]}/limit[<num>]/fields[<field1>,{<field2>}]/order[<field>]
    +
  • /search/format[xml]/query[<field><=|~><value>{(and|or)...]}/limit[<num>]/fields[<field1>,{<field2>}]/order[<field>]

    Example

    Search for entries with a call beginning with M0 and a locator beginning with I or J, show the callsign and locator fields, order it by callsign and limit the results to 10. -
  • /search/query[Call~M0*(and)(Locator~I*(or)Locator~J*)]/limit[10]/fields[distinct(Call),Locator]/order[Call(asc)]
    -
  • Run it! +
  • /search/format[xml]/query[Call~M0*(and)(Locator~I*(or)Locator~J*)]/limit[10]/fields[distinct(Call),Locator]/order[Call(asc)]
    +
  • Run it! (XML) or Run it! (JSON) diff --git a/application/views/api/index.php b/application/views/api/index.php index 725187b7..83c63382 100644 --- a/application/views/api/index.php +++ b/application/views/api/index.php @@ -1,8 +1,5 @@ saveXML()); +// Output + +// Check whether we want XML or JSON output +if($data['format'] == "xml") { + // Set the content-type for browsers + header("Content-type: text/xml"); + echo formatXmlString($xmlDoc->saveXML()); +} else if($data['format'] == "json") { + // Set the content-type for browsers + header("Content-type: application/json"); + // For now, our JSON output is simply the XML re-parsed with SimpleXML and + // then re-encoded with json_encode + $x = simplexml_load_string($xmlDoc->saveXML()); + $j = json_encode($x); + echo $j; +} else { + echo "Error: Unknown format type '".$data['format']."'."; +} // This function tidies up the outputted XML function formatXmlString($xml) {