Created
November 6, 2012 04:49
-
-
Save hSammir/4022647 to your computer and use it in GitHub Desktop.
Rest web service dengan memanfaatkan Slim Framework.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require 'Slim/Slim.php'; | |
| \Slim\Slim::registerAutoloader(); | |
| $app = new \Slim\Slim(); | |
| // route | |
| $app->get("/blog/post", function() use($app) { | |
| // tampilkan semua post | |
| $sql = "select * from post"; | |
| try{ | |
| $db = getConnection(); | |
| $stmt = $db->query($sql); | |
| $posts = $stmt->fetchAll(PDO::FETCH_OBJ); | |
| $db = null; | |
| echo '{"post": ' . json_encode($posts) . '}'; | |
| $app->response()->status(200); | |
| }catch(PDOException $e){ | |
| $app->response()->status(400); | |
| echo '{"error":{"text":'. $e->getMessage() .'}}'; | |
| } | |
| }); | |
| $app->post("/blog/post", function() use($app) { | |
| // tambahkan post baru | |
| $req = \Slim\Slim::getInstance()->request(); | |
| $sql = "INSERT INTO post(post) VALUES(:post)"; | |
| $post = $req->params("post"); | |
| try{ | |
| $db = getConnection(); | |
| $stmt = $db->prepare($sql); | |
| $stmt->bindParam("post", $post); | |
| $stmt->execute(); | |
| $db = null; | |
| $app->response()->status(201); | |
| } catch(PDOException $e){ | |
| $app->response()->status(400); | |
| echo '{"error":{"text":'. $e->getMessage() .'}}'; | |
| } | |
| }); | |
| $app->get("/blog/post/:id", function($id) use($app) { | |
| // tampilkan post dengan :id | |
| $req = \Slim\Slim::getInstance()->request(); | |
| $sql = "SELECT * FROM post WHERE id=:id"; | |
| try{ | |
| $db = getConnection(); | |
| $stmt = $db->prepare($sql); | |
| $stmt->bindParam("id", $id); | |
| $stmt->execute(); | |
| $post = $stmt->fetchObject(); | |
| $db = null; | |
| echo json_encode($post); | |
| $app->response()->status(200); | |
| } catch(PDOException $e){ | |
| $app->response()->status(400); | |
| echo '{"error":{"text":'. $e->getMessage() .'}}'; | |
| } | |
| }); | |
| $app->put("/blog/post/:id", function($id) use($app){ | |
| // edit post dengan :id | |
| $req = \Slim\Slim::getInstance()->request(); | |
| $sql = "UPDATE post SET post=:post where id=:id"; | |
| $post = $req->params("post"); | |
| try{ | |
| $db = getConnection(); | |
| $stmt = $db->prepare($sql); | |
| $stmt->bindParam("post", $post ); | |
| $stmt->bindParam("id", $id ); | |
| $stmt->execute(); | |
| $db = null; | |
| $app->response()->status(200); | |
| } catch(PDOException $e){ | |
| echo '{"error":{"text":'. $e->getMessage() .'}}'; | |
| $app->response()->status(404); | |
| } | |
| }); | |
| $app->delete("/blog/post/:id", function($id) use($app) { | |
| // delete post dengan :id | |
| $sql = "DELETE FROM post WHERE id=:id"; | |
| try{ | |
| $db = getConnection(); | |
| $stmt = $db->prepare($sql); | |
| $stmt->bindParam("id", $id); | |
| $stmt->execute(); | |
| $db = null; | |
| $app->response()->status(200); | |
| }catch(PDOException $e){ | |
| $app->response()->status(404); | |
| echo '{"error":{"text":'. $e->getMessage() .'}}'; | |
| } | |
| }); | |
| function getConnection() { | |
| $dbhost="127.0.0.1"; | |
| $dbuser="root"; | |
| $dbpass=""; | |
| $dbname="hdd"; | |
| $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); | |
| $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
| return $dbh; | |
| } | |
| $app->run() | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment