altitud.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. header('Access-Control-Allow-Origin: *');
  3. header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
  4. header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
  5. header("Allow: GET, POST, OPTIONS, PUT, DELETE");
  6. if($_SERVER['REQUEST_METHOD'] == "POST"){
  7. if(empty($_POST)){
  8. echo json_encode(array(
  9. "status" => "error",
  10. "results" => "Por favor ingresa los campos necesarios."
  11. ));
  12. exit;
  13. }
  14. if(!isset($_POST["LAT"])){
  15. echo json_encode(array(
  16. "status" => "error",
  17. "results" => "El LAT es un campo necesario."
  18. ));
  19. exit;
  20. }
  21. if(!isset($_POST["LON"])){
  22. echo json_encode(array(
  23. "status" => "error",
  24. "results" => "El LON es un campo necesario."
  25. ));
  26. exit;
  27. }
  28. try{
  29. $ch = curl_init();
  30. if($ch === false){
  31. echo json_encode(array(
  32. "status" => "error",
  33. "results" => "Fallo al inicializar CURL."
  34. ));
  35. exit;
  36. }
  37. $lat = $_POST["LAT"];
  38. $lon = $_POST["LON"];
  39. $url = "https://api.opentopodata.org/v1/test-dataset?locations=$lat,$lon";
  40. curl_setopt($ch, CURLOPT_URL, $url);
  41. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  42. $out = curl_exec($ch);
  43. if($out === false){
  44. echo json_encode(array(
  45. "status" => "error",
  46. "results" => 'Error: ' . curl_errno($ch) . ". Msg: " . curl_error($ch)
  47. ));
  48. exit;
  49. }
  50. echo $out;
  51. }catch(Exception $e){
  52. echo json_encode(array(
  53. "status" => "error",
  54. "results" => "Error al ejecutar la petición."
  55. ));
  56. }finally{
  57. if (is_resource($ch)) {
  58. curl_close($ch);
  59. }
  60. }
  61. }else{
  62. echo json_encode(array(
  63. "status" => "error",
  64. "results" => "Bad Request."
  65. ));
  66. }