PageRenderTime 27ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/www/docs/api/index.php

https://github.com/sebbacon/theyworkforyou
PHP | 251 lines | 227 code | 21 blank | 3 comment | 36 complexity | ece6893464d9a9979d2f815b1c51ec3e MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. <?
  2. include_once '../../includes/easyparliament/init.php';
  3. include_once '../../includes/postcode.inc';
  4. include_once 'api_functions.php';
  5. # XXX: Need to override error handling! XXX
  6. if ($q_method = get_http_var('method')) {
  7. if (get_http_var('docs')) {
  8. $key = 'DOCS';
  9. } else {
  10. $key = get_http_var('key');
  11. if (!$key) {
  12. api_error('No API key provided. Please see http://www.theyworkforyou.com/api/key for more information.');
  13. exit;
  14. }
  15. $check = api_check_key($key);
  16. if (!$check) {
  17. api_error('Invalid API key.');
  18. exit;
  19. } elseif ($check === 'disabled') {
  20. api_error('Your API key has been disabled.');
  21. exit;
  22. }
  23. }
  24. $match = 0;
  25. foreach ($methods as $method => $data) {
  26. if (strtolower($q_method) == strtolower($method)) {
  27. if (isset($data['superuser']) && $data['superuser']){
  28. $super_check = api_is_superuser_key($key);
  29. if (!$super_check) {
  30. if (get_http_var('docs')) {
  31. api_front_page();
  32. }else{
  33. api_error('Invalid API key.');
  34. exit;
  35. }
  36. }
  37. }
  38. api_log_call($key);
  39. $match++;
  40. if (get_http_var('docs')) {
  41. $_GET['verbose'] = 1;
  42. ob_start();
  43. }
  44. foreach ($data['parameters'] as $parameter) {
  45. if ($q_param = trim(get_http_var($parameter))) {
  46. $match++;
  47. include_once 'api_' . $method . '.php';
  48. api_call_user_func_or_error('api_' . $method . '_' . $parameter, array($q_param), 'API call not yet functional', 'api');
  49. break;
  50. }
  51. }
  52. if ($match == 1 && (get_http_var('output') || !get_http_var('docs'))) {
  53. if ($data['required']) {
  54. api_error('No parameter provided to function "' .
  55. htmlspecialchars($q_method) .
  56. '". Possible choices are: ' .
  57. join(', ', $data['parameters']) );
  58. } else {
  59. include_once 'api_' . $method . '.php';
  60. api_call_user_func_or_error('api_' . $method, null, 'API call not yet functional', 'api');
  61. break;
  62. }
  63. }
  64. break;
  65. }
  66. }
  67. if (!$match) {
  68. api_log_call($key);
  69. api_front_page('Unknown function "' . htmlspecialchars($q_method) .
  70. '". Possible functions are: ' .
  71. join(', ', array_keys($methods)) );
  72. } else {
  73. if (get_http_var('docs')) {
  74. $explorer = ob_get_clean();
  75. api_documentation_front($method, $explorer);
  76. }
  77. }
  78. } else {
  79. api_front_page();
  80. }
  81. function api_documentation_front($method, $explorer) {
  82. global $PAGE, $this_page, $DATA, $methods;
  83. $this_page = 'api_doc_front';
  84. $DATA->set_page_metadata($this_page, 'title', "$method function");
  85. $PAGE->page_start();
  86. $PAGE->stripe_start();
  87. include_once 'api_' . $method . '.php';
  88. print '<p align="center"><strong>http://www.theyworkforyou.com/api/' . $method . '</strong></p>';
  89. api_call_user_func_or_error('api_' . $method . '_front', null, 'No documentation yet', 'html');
  90. ?>
  91. <h4>Explorer</h4>
  92. <p>Try out this function without writing any code!</p>
  93. <form method="get" action="?#output">
  94. <p>
  95. <? foreach ($methods[$method]['parameters'] as $parameter) {
  96. print $parameter . ': <input type="text" name="'.$parameter.'" value="';
  97. if ($val = get_http_var($parameter))
  98. print htmlspecialchars($val);
  99. print '" size="30"><br>';
  100. }
  101. ?>
  102. Output:
  103. <input id="output_js" type="radio" name="output" value="js"<? if (get_http_var('output')=='js' || !get_http_var('output')) print ' checked'?>>
  104. <label for="output_js">JS</label>
  105. <input id="output_xml" type="radio" name="output" value="xml"<? if (get_http_var('output')=='xml') print ' checked'?>>
  106. <label for="output_xml">XML</label>
  107. <input id="output_php" type="radio" name="output" value="php"<? if (get_http_var('output')=='php') print ' checked'?>>
  108. <label for="output_php">Serialised PHP</label>
  109. <input id="output_rabx" type="radio" name="output" value="rabx"<? if (get_http_var('output')=='rabx') print ' checked'?>>
  110. <label for="output_rabx">RABX</label>
  111. <input type="submit" value="Go">
  112. </p>
  113. </form>
  114. <?
  115. if ($explorer) {
  116. $qs = array();
  117. foreach ($methods[$method]['parameters'] as $parameter) {
  118. if (get_http_var($parameter))
  119. $qs[] = htmlspecialchars(rawurlencode($parameter) . '=' . urlencode(get_http_var($parameter)));
  120. }
  121. print '<h4><a name="output"></a>Output</h4>';
  122. print '<p>URL for this: <strong>http://www.theyworkforyou.com/api/';
  123. print $method . '?' . join('&amp;', $qs) . '&amp;output='.get_http_var('output').'</strong></p>';
  124. print '<pre>' . htmlspecialchars($explorer) . '</pre>';
  125. }
  126. $sidebar = api_sidebar();
  127. $PAGE->stripe_end(array($sidebar));
  128. $PAGE->page_end();
  129. }
  130. function api_front_page($error = '') {
  131. global $PAGE, $methods, $this_page, $THEUSER;
  132. $this_page = 'api_front';
  133. $PAGE->page_start();
  134. $PAGE->stripe_start();
  135. if ($error) {
  136. print "<p style='color: #cc0000'>$error</p>";
  137. }
  138. ?>
  139. <p>Welcome to TheyWorkForYou's API section, where you can learn how to query our database for information.</p>
  140. <h3>Overview</h3>
  141. <ol style="font-size:130%">
  142. <li>
  143. <? if ($THEUSER->loggedin()) { ?>
  144. <a href="key">Get an API key (or view stats of existing keys)</a>.
  145. <? } else { ?>
  146. <a href="key">Get an API key</a>.
  147. <? } ?>
  148. <li>All requests are made by GETting a particular URL with a number of parameters. <em>key</em> is required;
  149. <em>output</em> is optional, and defaults to <kbd>js</kbd>.
  150. </ol>
  151. <p align="center"><strong>http://www.theyworkforyou.com/api/<em>function</em>?key=<em>key</em>&amp;output=<em>output</em>&amp;<em>other_variables</em></strong></p>
  152. <? api_key_current_message(); ?>
  153. <p>The current version of the API is <em>1.0.0</em>. If we make changes to the
  154. API functions, we'll increase the version number and make it an argument so you
  155. can still use the old version.</p>
  156. <table>
  157. <tr valign="top">
  158. <td width="60%">
  159. <h3>Outputs</h3>
  160. <p>The <em>output</em> argument can take any of the following values:
  161. <ul>
  162. <li><strong>xml</strong>. XML. The root element is twfy.</li>
  163. <li><strong>php</strong>. Serialized PHP, that can be turned back into useful information with the unserialize() command. Quite useful in Python as well, using <a href="http://hurring.com/code/python/serialize/">PHPUnserialize</a>.</li>
  164. <li><strong>js</strong>. A JavaScript object. You can provide a callback
  165. function with the <em>callback</em> variable, and then that function will be
  166. called with the data as its argument.</li>
  167. <li><strong>rabx</strong>. "RPC over Anything But XML".</li>
  168. </ul>
  169. </td><td>
  170. <h3>Errors</h3>
  171. <p>If there's an error, either in the arguments provided or in trying to perform the request,
  172. this is returned as a top-level error string, ie. in XML it returns
  173. <code>&lt;twfy&gt;&lt;error&gt;ERROR&lt;/error&gt;&lt;/twfy&gt;</code>;
  174. in JS <code>{"error":"ERROR"}</code>;
  175. and in PHP and RABX a serialised array containing one entry with key <code>error</code>.
  176. </td></tr></table>
  177. <h3>Licensing</h3>
  178. <p>To use parliamentary material yourself (that's data returned from
  179. getDebates, getWrans, and getWMS), you will need to get a
  180. <a href="http://www.opsi.gov.uk/click-use/parliamentary-licence-information/index.htm">Parliamentary Licence</a> from the Office of Public Sector
  181. Information.
  182. <? /* All Ordnance Survey data (returned by getGeometry and getBoundary) is
  183. covered by the <acronym title="Pan-Government Agreement">PGA</acronym>
  184. under the <a href="http://www.dca.gov.uk/">Department for Constitutional
  185. Affairs</a>; you will have to get your own licence to re-use them. */ ?>
  186. Our own data - lists of MPs, Lords, constituencies and so on - is available
  187. under the <a href="http://creativecommons.org/licenses/by-sa/2.5/">Creative
  188. Commons Attribution-ShareAlike license version 2.5</a>.
  189. <p>Low volume, non-commercial use of the API service itself is free. Please
  190. <a href="/contact">contact us</a> for commercial use, or if you are about
  191. to use the service on a large scale. Please credit TheyWorkForYou if you
  192. use the API.
  193. <h3>Bindings</h3>
  194. <p>These help you interface with the API more easily in a particular language:</p>
  195. <ul>
  196. <li><a href="http://codefluency.com/articles/2006/11/21/tfwy-1-0-0-released/">Ruby</a> (thanks to Bruce Williams and Martin Owen)</li>
  197. <li><a href="http://search.cpan.org/perldoc?WebService::TWFY::API">Perl</a> (thanks to Spiros Denaxas)</li>
  198. <li><a href="http://tools.rubenarakelyan.com/twfyapi/">PHP</a> (thanks to Ruben Arakelyan)</li>
  199. <li><a href="http://code.google.com/p/twfython/">Python</a> (thanks to Paul Doran)</li>
  200. <li><a href="http://tools.rubenarakelyan.com/twfyapi/">ASP.net</a> (thanks to Ruben Arakelyan)</li>
  201. <li><a href="https://sourceforge.net/projects/twfyjavaapi">Java</a> (thanks to Mitch Kent)</li>
  202. </ul>
  203. <p>If anyone wishes to write bindings for the API in any language, please
  204. do so, let us know and we'll link to it here. You might want to
  205. <a href="https://secure.mysociety.org/admin/lists/mailman/listinfo/developers-public">join our public developer mailing list</a>
  206. to discuss things.</p>
  207. <h3>Examples</h3>
  208. <ul>
  209. <li><a href="http://www.dracos.co.uk/work/theyworkforyou/api/postcode/">Postcode to constituency lookup, with no server side code</a> - use this to add constituency or MP lookup to a form on your website.
  210. <li><a href="http://www.dracos.co.uk/work/theyworkforyou/api/map/">Map showing location of all 646 constituencies, with no server side code</a> - example code using JavaScript and Google Maps.
  211. <li><a href="javascript:function foo(r){if(r.twfy.url)window.location=r.twfy.url;};(function(){var s=document.createElement('script');s.setAttribute('src','http://theyworkforyou.com/api/convertURL?key=Gbr9QgCDzHExFzRwPWGAiUJ5&callback=foo&url='+encodeURIComponent(window.location));s.setAttribute('type','text/javascript');document.getElementsByTagName('head')[0].appendChild(s);})()">Hansard prettifier</a> - drag this bookmarklet to your bookmarks bar, or bookmark it. Then if you ever find yourself on the official site, clicking this will try and take you to the equivalent page on TheyWorkForYou. (Tested in IE, Firefox, Opera.)</li>
  212. <li><a href="http://www.dracos.co.uk/work/theyworkforyou/api/fabfarts/">Matthew's MP Fab Farts</a> - every technology has the capacity to be used for fun.
  213. <li><a href="telnet://seagrass.goatchurch.org.uk:646/">Francis' MP Fight telnet text adventure</a> (<s>and <a href="http://caesious.beasts.org/~chris/scripts/mpfight">Chris' web version</a></s>) - battle your way to Sedgefield!
  214. <li><a href="http://www.straw-dogs.co.uk/10/15/your-mp-google-desktop-gadget/">Your MP - Google Desktop Gadget</a> - with GPL source code
  215. </ul>
  216. <?
  217. $sidebar = api_sidebar();
  218. $PAGE->stripe_end(array($sidebar));
  219. $PAGE->page_end();
  220. }