无法测试Rest api

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().'}}';
}
}
?>
dtnmuyoiw680512744
dtnmuyoiw680512744 好的,现在我删除了它如何在$body=$request->getBody()中访问这些值;
接近 6 年之前 回复
dongxiequ3724
dongxiequ3724 $user=json_decode($body);当你不是json的时候,为什么要解码呢?删除它
接近 6 年之前 回复
douqinlu4217
douqinlu4217 您在此处发布的代码不会处理您的输入...您的真正问题是什么?您想了解导致错误的REST客户端或PHP代码吗?
接近 6 年之前 回复
dpjjr42626
dpjjr42626 尝试过但它没有将这些值解析为我的post函数。这个输出我得到{“fname”:null,“email”:null,“id”:“25”}
接近 6 年之前 回复
dreamact3026
dreamact3026 那么您想知道如何使用ChromeREST客户端的post方法吗?
接近 6 年之前 回复

1个回答

Okay, here is my issue, when i echo the content of the $body in the code below these values are displayed "fname=mishael&email=mishaelharry%40gmail.com" but after i decode it with $user = json_decode($body); no values are inserted into my database and i get {"fname":null,"email":null,"id":"33"} from this line of code echo json_encode($user); [code]

function addUser() {
$request = \Slim\Slim::getInstance()->request();
$body = $request->getBody();
echo json_encode($body);
$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().'}}';
}
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐