2016-02-05 37 views
0

我有這個代碼用於處理用戶從輸入參數電子郵件和密碼的輸入。允許php函數處理原始JSON數據

$app->post('/login', function() use ($app) { 
      // 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["response_status"] =array('status_code'=>0, 'status_message' => 'Login Successfuly'); 
        $response["customer_creds"] =array(array('customer_names'=>$user['name'], 'customer_email' => $user['email'], 'customer_id' => $user['customer_id'], 'customer_type' => $user['customer_type'], 'rating' => $user['rating'])); 
        $response["customer_payment_instruments"] =array(array('payment_method'=>$user['payment_method1'], 'account_number' => $user['account_number1']),array('payment_method'=>$user['payment_method2'], 'account_number' => $user['account_number2']),array('payment_method'=>$user['payment_method3'], 'account_number' => $user['account_number3'])); 
        $response["customer_vehicle_details"] =array(array('vehicle_plate'=>$user['vehicle_plate1'], 'vehicle_make' => $user['vehicle_make1'], 'vehicle_model' => $user['vehicle_model1'], 'vehicle_colour' => $user['vehicle_colour1'], 'vehicle_id' => $user['vehicle_id1']), array('vehicle_plate'=>$user['vehicle_plate2'], 'vehicle_make' => $user['vehicle_make2'], 'vehicle_model' => $user['vehicle_model2'], 'vehicle_colour' => $user['vehicle_colour2'], 'vehicle_id' => $user['vehicle_id2'])); 

       } else { 
        // unknown error occurred 
        $response["response_status"] =array('status_code'=>1, 'status_message' => 'An unknown error occurred. Please try again'); 
        //$response['error'] = true; 
        //$response['message'] = "An error occurred. Please try again"; 
       } 
      } else { 
       // user credentials are wrong 
       $response["response_status"] =array('status_code'=>1, 'status_message' => 'Login failed. Incorrect credentials'); 
      } 

      echoRespnse(200, $response); 
     }); 

該腳本檢查電子郵件和密碼是否存在於數據庫中並給出json響應。

public function checkLogin ($email, $password) { 
    // fetching user by email 
    $stmt = $this->conn->prepare("SELECT password_hash FROM users WHERE email = ?"); 

    $stmt->bind_param("s", $email); 

    $stmt->execute(); 

    $stmt->bind_result ($password_hash); 

    $stmt->store_result(); 

    if ($stmt->num_rows > 0) { 
     // Found user with the email 
     // Now verify the password 

     $stmt->fetch(); 

     $stmt->close(); 

     if (PassHash::check_password($password_hash, $password)) { 
      // User password is correct 
      return TRUE; 
     } else { 
      // user password is incorrect 
      return FALSE; 
     } 
    } else { 
     $stmt->close(); 

     // user not existed with the email 
     return FALSE; 
    } 
} 


/** 
* Fetching user by email 
* @param String $email User email id 
*/ 
public function getUserByEmail($email) { 
    $stmt = $this->conn->prepare("SELECT name, email, customer_id, customer_type, rating, payment_method1, account_number1, payment_method2, account_number2, payment_method3, account_number3, vehicle_plate1, vehicle_make1, vehicle_model1, vehicle_colour1, vehicle_id1, vehicle_plate2, vehicle_make2, vehicle_model2, vehicle_colour2, vehicle_id2, api_key, status, created_at FROM users WHERE email = ?"); 
    $stmt->bind_param("s", $email); 
    if ($stmt->execute()) { 
     $user = $stmt->get_result()->fetch_assoc(); 
     $stmt->close(); 
     return $user; 
    } else { 
     return NULL; 
    } 
} 

我現在試圖找出一種允許人們解析json數據的方法。這樣做的任何workarouds?目前,腳本只接受表單中的數據。是否有允許格式

{ 
"email":"", 
"password":"" 
} 
+0

您希望用戶能夠通過兩者登錄,或者現在只需要將其更改爲原始數據? –

+0

@Sagar我想只使用原始數據 – nick

回答

1

這裏原始數據的方式,你可以在你當前的代碼使用代碼:

替換此代碼:

// reading post params 
$email = $app->request()->post('email'); 
$password = $app->request()->post('password'); 

隨着

// reading request body 
$requestBody = $app->request->getBody(); 
// parsing json data to php 
$requestData = json_decode($requestBody, 1); 
// checking if email or password is set, if not the return response 
if(empty($requestData['email']) || empty($requestData['password'])) { 
    echoRespnse(400, ['message' => 'username/password cannot be empty']); 
} 

$email = $requestData['email']; 
$password = $requestData['password']; 

希望這應該工作!

+0

這不起作用 – nick

+0

你會得到什麼錯誤? –