Added JSON output functionality to API calls (see format[xml] or format[json]).

这个提交包含在:
Andy Smith 2012-01-01 23:15:11 +00:00
父节点 fe14cdc111
当前提交 c9e43ec203
共有 3 个文件被更改,包括 27 次插入10 次删除

查看文件

@ -148,6 +148,7 @@ class API extends CI_Controller {
// Retrieve the arguments from the query string // Retrieve the arguments from the query string
$arguments = $this->_retrieve(); $arguments = $this->_retrieve();
$data['data']['format'] = $arguments['format'];
// Call the parser within the API model to build the query // Call the parser within the API model to build the query
$query = $this->api_model->select_parse($arguments); $query = $this->api_model->select_parse($arguments);
@ -244,6 +245,7 @@ class API extends CI_Controller {
$limit = preg_grep("/^limit\[(.*)\]$/", $this->uri->segments); $limit = preg_grep("/^limit\[(.*)\]$/", $this->uri->segments);
$order = preg_grep("/^order\[(.*)\]$/", $this->uri->segments); $order = preg_grep("/^order\[(.*)\]$/", $this->uri->segments);
$fields = preg_grep("/^fields\[(.*)\]$/", $this->uri->segments); $fields = preg_grep("/^fields\[(.*)\]$/", $this->uri->segments);
$format = preg_grep("/^format\[(.*)\]$/", $this->uri->segments);
// Strip each argument // Strip each argument
$arguments['query'] = substr(array_pop($query), 6); $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['order'] = substr($arguments['order'], 0, strlen($arguments['order']) - 1);
$arguments['fields'] = substr(array_pop($fields), 7); $arguments['fields'] = substr(array_pop($fields), 7);
$arguments['fields'] = substr($arguments['fields'], 0, strlen($arguments['fields']) - 1); $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 the arguments
return $arguments; return $arguments;

查看文件

@ -81,15 +81,15 @@
<li><a href="<?php echo site_url('api/generate/rw'); ?>">Key with Read & Write Access</a></li> <li><a href="<?php echo site_url('api/generate/rw'); ?>">Key with Read & Write Access</a></li>
<li><a href="<?php echo site_url('api/generate/r'); ?>">Key with Read Only Access</a></li> <li><a href="<?php echo site_url('api/generate/r'); ?>">Key with Read Only Access</a></li>
</ul> </ul>
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.
<h3>API Guide</h3> <h3>API Guide</h3>
<h4>Description</h4> <h4>Description</h4>
Query the logbook Query the logbook, and output in XML format.
<h4>Syntax</h4> <h4>Syntax</h4>
<li><pre>/search/query[&lt;field&gt;&lt;=|~&gt;&lt;value&gt;{(and|or)...]}/limit[&lt;num&gt;]/fields[&lt;field1&gt;,{&lt;field2&gt;}]/order[&lt;field&gt;]</pre> <li><pre>/search/format[xml]/query[&lt;field&gt;&lt;=|~&gt;&lt;value&gt;{(and|or)...]}/limit[&lt;num&gt;]/fields[&lt;field1&gt;,{&lt;field2&gt;}]/order[&lt;field&gt;]</pre>
<h4>Example</h4> <h4>Example</h4>
Search for entries with a call beginning with <b>M0</b> and a locator beginning with <b>I</b> or <b>J</b>, show the callsign and locator fields, order it by callsign and limit the results to <b>10</b>. Search for entries with a call beginning with <b>M0</b> and a locator beginning with <b>I</b> or <b>J</b>, show the callsign and locator fields, order it by callsign and limit the results to <b>10</b>.
<li><pre>/search/query[Call~M0*(and)(Locator~I*(or)Locator~J*)]/limit[10]/fields[distinct(Call),Locator]/order[Call(asc)]</pre> <li><pre>/search/format[xml]/query[Call~M0*(and)(Locator~I*(or)Locator~J*)]/limit[10]/fields[distinct(Call),Locator]/order[Call(asc)]</pre>
<li><a href="<?php echo site_url('/api/search/query[Call~M0*(and)(Locator~I*(or)Locator~J*)]/limit[10]/fields[distinct(Call),Locator]/order[Call(asc)]'); ?>">Run it!</a> <li><a href="<?php echo site_url('/api/search/format[xml]/query[Call~M0*(and)(Locator~I*(or)Locator~J*)]/limit[10]/fields[distinct(Call),Locator]/order[Call(asc)]'); ?>">Run it! (XML)</a> or <a href="<?php echo site_url('/api/search/format[json]/query[Call~M0*(and)(Locator~I*(or)Locator~J*)]/limit[10]/fields[distinct(Call),Locator]/order[Call(asc)]'); ?>">Run it! (JSON)</a>
</div> </div>

查看文件

@ -1,8 +1,5 @@
<?php <?php
// Set the content-type for browsers
header("Content-type: text/xml");
// Create the DOMDocument for the XML output // Create the DOMDocument for the XML output
$xmlDoc = new DOMDocument("1.0"); $xmlDoc = new DOMDocument("1.0");
// Add reference to the XSLT // Add reference to the XSLT
@ -70,8 +67,24 @@ if($output['results'])
} }
} }
// Output formatted XML // 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()); 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 // This function tidies up the outputted XML
function formatXmlString($xml) { function formatXmlString($xml) {