允许php函数处理原始json数据

时间:2016-02-05 09:41:41

标签: php json slim

我有这个代码,用于处理来自from的用户输入和参数email和密码。

$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数据的方法。这样做有什么工作吗?目前,脚本只接受表单中的数据。有没有办法允许格式

的原始数据
{
"email":"",
"password":""
}

1 个答案:

答案 0 :(得分: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'];

希望这应该有用!!