PageRenderTime 134ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/api/index.php

https://gitlab.com/ualmtorres/OSM_APIREST
PHP | 107 lines | 58 code | 29 blank | 20 comment | 0 complexity | b7c8be51e595ba46c5a88c340e4e398f MD5 | raw file
  1. <?php
  2. // Instantiate the class responsible for implementing a micro application
  3. $app = new \Phalcon\Mvc\Micro();
  4. // Routes
  5. $app->get('/', 'home');
  6. $app->get('/api', 'home');
  7. $app->get('/api/amenity/{amenity}', 'findAmenities'); // curl -i -X GET http://localhost/MongoDBBlog/api/api/id/50ab0f8bbcf1bfe2536dc3f8
  8. $app->get('/api/bar/{bar}', 'findBar'); // curl -i -X GET http://localhost/MongoDBBlog/api/api/tag/trade
  9. $app->get('/api/amenity/{amenity}/lat/{lat}/lon/{lon}/radius/{radius}', 'findAmenities100MetersNear'); // curl -i -X POST -d '{"body":"Lore ipsum", "permalink": "TqoHkbHyUgLyCKWgPLqm", "author": "machine", "title": "Lore ipsum", "tags": ["Lore", "ipsum"], "comments":[{"body": "Lore ipsum", "email": "john@doe.com", "author": "John Doe"}]}' http://localhost/MongoDBBlog/api/api
  10. //$app->get('/api/amenity/{amenity}/lat/{lat}/lon/{lon}/radius{radius}', 'findAmenities100MetersNear'); // curl -i -X POST -d '{"body":"Lore ipsum", "permalink": "TqoHkbHyUgLyCKWgPLqm", "author": "machine", "title": "Lore ipsum", "tags": ["Lore", "ipsum"], "comments":[{"body": "Lore ipsum", "email": "john@doe.com", "author": "John Doe"}]}' http://localhost/MongoDBBlog/api/api
  11. //http://localhost/OSM_REST/api/api/amenity/bar/lat/36.8388993/lon/-2.464748/r/100
  12. $app->get('/api/llamadaXQ', 'llamadaXQ'); //
  13. $app->get('/api/dameREST', 'dameREST'); //
  14. $app->notFound('notFound');
  15. // Handlers
  16. // Show the use of the API
  17. function home() {
  18. header('Location:../useOfTheAPI.php');
  19. }
  20. //Searches for amenities (i.e. bar, restaurant)
  21. function findAmenities ($amenity) {
  22. include("../connection.php");
  23. $fullurl = $urlPrefix . 'query=%3CBars%3E%20{%20for%20$node%20in%20collection(%22osm%22)/osm/node%20where%20$node/tag[@k=%22amenity%22]%20and%20$node/tag[@v=%22' . $amenity . '%22]%20return%20%3Cbar%3E%20{$node}%20%3C/bar%3E%20}%20%3C/Bars%3E';
  24. renderXML($fullurl);
  25. }
  26. //Searches for a bar (i.e. Cuore, Campanilla)
  27. function findBar ($bar) {
  28. include("../connection.php");
  29. $fullurl = $urlPrefix . 'query=%3CBars%3E%20{%20for%20$node%20in%20collection(%22osm%22)/osm/node%20where%20$node/tag[@k=%22amenity%22]%20and%20$node/tag[@v=%22bar%22]%20and%20$node/tag[@k=%22name%22]%20and%20$node/tag[@v=%22' . $bar . '%22]%20return%20%3Cbar%3E%20{$node[@lat]}%20%3C/bar%3E%20}%20%3C/Bars%3E';
  30. renderXML($fullurl);
  31. }
  32. //Searches for amenities that are 100 meters near from the (lat, lon) specified
  33. //function findAmenities100MetersNear($amenity, $lat, $lon, $radius) {
  34. //function findAmenities100MetersNear($amenity, $lat, $lon, $radius) {
  35. function findAmenities100MetersNear($amenity, $lat, $lon, $radius) {
  36. include("../connection.php");
  37. //$fullurl = $urlPrefix . 'query=%3CBars%3E%20{%20for%20$node%20in%20collection(%22osm%22)/osm/node%20where%20$node/tag/@k=%22amenity%22%20and%20$node/tag/@v=%22' . $amenity . '%22%20and%20$node/@lat%20%3E%20' . $lat . '%20-%200.00000898%20*%20100%20and%20$node/@lat%20%3C%20' . $lat . '%20-%20-0.00000898%20*%20100%20and%20$node/@lon%20%3E%20' . $lon . '%20-%200.00000898%20*%20100%20and%20$node/@lon%20%3C%20' . $lon . '%20-%20-0.00000898%20*%20100%20return%20%3Cbar%3E%20{$node}%20%3C/bar%3E%20}%20%3C/Bars%3E';
  38. $fullurl = $urlPrefix . 'query=%3CBars%3E%20{%20for%20$node%20in%20collection(%22osm%22)/osm/node%20where%20$node/tag/@k=%22amenity%22%20and%20$node/tag/@v=%22' . $amenity . '%22%20and%20$node/@lat%20%3E%20' . $lat . '%20-%200.00000898%20*%20' . $radius . '%20and%20$node/@lat%20%3C%20' . $lat . '%20-%20-0.00000898%20*%20' . $radius . '%20and%20$node/@lon%20%3E%20' . $lon . '%20-%200.00000898%20*%20' . $radius . '%20and%20$node/@lon%20%3C%20' . $lon . '%20-%20-0.00000898%20*%20' . $radius . '%20return%20%3Cbar%3E%20{$node}%20%3C/bar%3E%20}%20%3C/Bars%3E';
  39. //echo "Hoooola";
  40. renderXML($fullurl);
  41. }
  42. //Uses a XQ function
  43. function llamadaXQ() {
  44. $fullurl = 'http://localhost:8080/exist/rest/db/apps/osm/saludar.xq';
  45. $ch = curl_init();
  46. // Establecer URL y otras opciones apropiadas
  47. curl_setopt($ch, CURLOPT_URL, $fullurl);
  48. curl_setopt($ch, CURLOPT_HEADER, 0);
  49. // Capturar la URL y pasarla al navegador
  50. curl_exec($ch);
  51. // Cerrar el recurso cURL y liberar recursos del sistema
  52. curl_close($ch);
  53. }
  54. //Adds a new post
  55. function dameREST() {
  56. }
  57. function notFound() {
  58. home();
  59. }
  60. function renderXML($url) {
  61. include("../connection.php");
  62. // Create a stream
  63. $opts = array(
  64. 'http'=>array(
  65. 'method' => "GET",
  66. 'header' => "Authorization: Basic " .
  67. base64_encode("$userBaseX:$passwordBaseX")
  68. )
  69. );
  70. $context = stream_context_create($opts);
  71. $xml = file_get_contents($url, FALSE, $context);
  72. $sxe = new SimpleXMLElement($xml);
  73. Header('Content-type: text/xml');
  74. print($sxe->asXML());
  75. }
  76. // Handle the request
  77. $app->handle();
  78. ?>