i am developing a REST API for my android application. At the moment i haven't incoropated it into my application instead i am trying to test it with the popular "Advanced Rest Client App" on chrome. The function handling the GET method works fine and displays records from my db in json format but when i try to test the function handling POST it doesn't work. Please i would like some one to help me out on how to test my post function from "Advanced Rest Client App".
API Code:
<?php
require_once '../include/DbConnect.php';
require '.././libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
// create new Slim instance
$app = new \Slim\Slim();
$app->get('/users', 'getUser');
$app->post('/register', 'addUser');
$app->run();
function getUser(){
$sql = "SELECT * FROM users";
try{
$db = new DbConnect();
$conn = $db->connect();
$stmt = $conn->query($sql);
$users = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo '{"user": ', json_encode($users).'}';
} catch(PDOException $e){
echo '{"error":{"text":'. $e->getMessage().'}}';
}
}
function addUser() {
$request = \Slim\Slim::getInstance()->request();
$body = $request->getBody();
$user = json_decode($body);
$sql = "INSERT INTO users (fname, email) VALUES(:fname, :email)";
try{
$db = new DbConnect();
$conn = $db->connect();
$stmt = $conn->prepare($sql);
$stmt->bindParam("fname", $user->fname);
$stmt->bindParam("email", $user->email);
$stmt->execute();
$user->id = $conn->lastInsertId();
$db = null;
echo json_encode($user);
} catch(PDOException $e){
echo '{"error":{"text":'. $e->getMessage().'}}';
}
}
?>