Advertisement
Guest User

Untitled

a guest
Jul 11th, 2018
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. <?php
  2. header('Access-Control-Allow-Origin: *');
  3. require 'Slim/Slim.php';
  4. \Slim\Slim::registerAutoloader();
  5.  
  6. $app = new \Slim\Slim();
  7.  
  8. $app->get('/cursos', 'getCursos');
  9. $app->get('/cursos/:id', 'getCurso');
  10. $app->post('/cursos', 'insertar');
  11. $app->put('/cursos', 'actualizar');
  12. $app->delete('/cursos/:id', 'eliminar');
  13.  
  14. $app->run();
  15.  
  16. function getCursos() {
  17. $sql = "select * FROM curso ORDER BY nombre";
  18. try {
  19. $db = getConnection();
  20. $stmt = $db->query($sql);
  21. $wines = $stmt->fetchAll(PDO::FETCH_OBJ);
  22. $db = null;
  23. echo '{"cursos":' . json_encode($wines) . '}';
  24. } catch(PDOException $e) {
  25. echo '{"error":{"text":'. $e->getMessage() .'}}';
  26. }
  27. }
  28.  
  29. function actualizar() {
  30. $app = \Slim\Slim::getInstance();
  31. $curso = json_decode($app->request()->getBody());
  32. $sql = "UPDATE curso SET nombre=:nombre, descripcion=:descripcion, imagen=:imagen, duracion=:duracion WHERE idcurso = :idcurso";
  33. $db = getConnection();
  34. $stmt = $db->prepare($sql);
  35. $stmt->bindParam("idcurso", $curso->idcurso);
  36. $stmt->bindParam("nombre", $curso->nombre);
  37. $stmt->bindParam("descripcion", $curso->descripcion);
  38. $stmt->bindParam("imagen", $curso->imagen);
  39. $stmt->bindParam("duracion", $curso->duracion);
  40. $stmt->execute();
  41. $sql = "SELECT * FROM curso WHERE idcurso= :idcurso";
  42. $stmt = $db->prepare($sql);
  43. $stmt->bindParam("idcurso", $curso->idcurso);
  44. $stmt->execute();
  45. $curso = $stmt->fetchObject();
  46. echo json_encode($curso);
  47. }
  48.  
  49. function insertar() {
  50. $app = \Slim\Slim::getInstance();
  51. $curso = json_decode($app->request()->getBody());
  52. $sql = "INSERT INTO curso(nombre, descripcion,imagen,duracion) VALUES(:nombre,:descripcion,:imagen,:duracion)";
  53. $db = getConnection();
  54. $stmt = $db->prepare($sql);
  55. $stmt->bindParam("nombre", $curso->nombre);
  56. $stmt->bindParam("descripcion", $curso->descripcion);
  57. $stmt->bindParam("imagen", $curso->imagen);
  58. $stmt->bindParam("duracion", $curso->duracion);
  59. $stmt->execute();
  60. $sql = "SELECT * FROM curso WHERE idcurso= (SELECT MAX(idCurso) FROM curso)";
  61. $stmt = $db->prepare($sql);
  62. $stmt->execute();
  63. $curso = $stmt->fetchObject();
  64. echo json_encode($curso);
  65. }
  66.  
  67. function eliminar($id) {
  68. $sql = "DELETE FROM curso WHERE idcurso=:id";
  69. $db = getConnection();
  70. $stmt = $db->prepare($sql);
  71. $stmt->bindParam("id", $id);
  72. $stmt->execute();
  73. echo 'OK';
  74. }
  75. function getCurso($id) {
  76. $sql = "SELECT * FROM curso WHERE idcurso=:id";
  77. try {
  78. $db = getConnection();
  79. $stmt = $db->prepare($sql);
  80. $stmt->bindParam("id", $id);
  81. $stmt->execute();
  82. $wine = $stmt->fetchObject();
  83. $db = null;
  84. echo json_encode($wine);
  85. } catch(PDOException $e) {
  86. echo '{"error":{"text":'. $e->getMessage() .'}}';
  87. }
  88. }
  89.  
  90. function getConnection() {
  91. $dbhost="tgrest.db.10810290.hostedresource.com";
  92. $dbuser="tgrest";
  93. $dbpass="TecFest#2015";
  94. $dbname="tgrest";
  95. $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
  96. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  97. return $dbh;
  98. }
  99.  
  100. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement