2016-09-12 52 views
-1

我是新來的苗條框架下面的教程我設法獲取發佈數據到我的API。但是當我試圖發送數據爲JSON它給了我一個error.I試圖加入JSON請求作爲遵循什麼是正確的語法才達到this.i得到錯誤爲必填字段(S)的姓名,電子郵件,密碼丟失或空在瘦框架中讀取JSON輸入

$app->post('/login', function() use ($app) { 
       // check for required params 
       $json = $app->request->getBody(); 
       $data = json_decode($json, true); 
       verifyRequiredParams(array('name','email', 'password')); 

我怎麼能在我的API POST請求從獲取JSON數據JSON陣列等

{ 
"name":"usertest", 
"email":"[email protected]", 
"password":"xxxxxx" 
} 

我可以使用verifyRequiredParams(array('name','email', 'password'));$name = $app->request->post('name');如果請求共我作爲JSON。

+0

你的意思是要變換數組JSON? – rbr94

+0

對不起,如果我使用了錯誤terms.my擔心是,如果我們發送一個數據的JSON數組在後方法如何閱讀它,如$ email = $ app-> request-> post('email')是否一樣?當我嘗試驗證參數時,我認爲錯誤即將到來。我會添加一個樣本發佈數組問題。 –

+0

我添加了一個解決方案。如果它不能解決您的問題,請發佈'$ data'的內容(例如'print_r($ data);') – rbr94

回答

2

要讀取請求數據,您可以使用您的$data屬性。它應該是一個對象,所以你可以使用它像這樣:

$name = $data->name; 
$email = $data->email; 

編輯:

使用$data = json_decode($json),而不是$data = json_decode($json, true)的JSON數據轉換爲對象,而不是關聯數組。

0

問題出在放置我的verifyRequiredParams函數我think.i解決了以下代碼的問題,如果任何人有同樣的問題。

$app->post('/login', function() use ($app) { 
      if($app->request->headers->get('Content-Type')=='application/json'){ 
       $json = $app->request->getBody(); 
       verifyRequiredParamsjson(array('email','password'),$json); 
       $data = json_decode($json); 

       // check for required params        

       $email = $data->email; 
       $password = $data->password; 
      } 
      else{ 
       // check for required params 
       verifyRequiredParams(array('email', 'password')); 
       // reading post params 
       $email = $app->request->post('email'); 
       $password = $app->request->post('password'); 
      } 

      $response = array(); 

      $db = new DbHandler(); 
      // check for correct email and password 
      if ($db->checkLogin($email, $password)) { 
       // get the user by email 
       $user = $db->getUserByEmail($email); 

       if ($user != NULL) { 
        $response["error"] = false; 
        $response['name'] = $user['name']; 
        $response['email'] = $user['email']; 
        $response['apiKey'] = $user['api_key']; 
        $response['createdAt'] = $user['created_at']; 
       } else { 
        // unknown error occurred 
        $response['error'] = true; 
        $response['message'] = "An error occurred. Please try again"; 
       } 
      } 
      else{ 
        $response['error'] = true; 
        $response['message'] = 'Login failed. Incorrect credentials'; 
       } 

      echoRespnse(200, $response); 
     }); 

所需的參數檢查,

function verifyRequiredParams($required_fields) { 
    $error = false; 
    $error_fields = ""; 
    $request_params = array(); 
    $request_params = $_REQUEST; 
    // Handling PUT request params 
    if ($_SERVER['REQUEST_METHOD'] == 'PUT') { 
     $app = \Slim\Slim::getInstance(); 
     parse_str($app->request()->getBody(), $request_params); 
    } 
    foreach ($required_fields as $field) { 
     if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) { 
      $error = true; 
      $error_fields .= $field . ', '; 
     } 
    } 

    if ($error) { 
     // Required field(s) are missing or empty 
     // echo error json and stop the app 
     $response = array(); 
     $app = \Slim\Slim::getInstance(); 
     $response["error"] = true; 
     $response["message"] = 'Required field(s) ' . substr($error_fields, 0, -2) . ' is missing or empty'; 
     echoRespnse(400, $response); 
     $app->stop(); 
    } 
}