我使用PHP创建了一个REST API,使用JsonObjectRequest
从我的Android应用程序接收数据。我正在使用Postman
中的以下网址调用API,但我的PHP应用程序告诉我它没有收到params
网址:http://192.168.2.15/login_api/login.php?email=test@test.com&password=test
这是我的login.php
:
<?php
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
$response = array("error" => FALSE);
if (isset($_POST['email']) && isset($_POST['password'])) {
$email = $_POST['email'];
$password = $_POST['password'];
$user = $db->getUserByEmail($email, $password);
if ($user != FALSE) {
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["date_created"] = $user["date_created"];
echo json_encode($response);
} else {
$response["error"] = TRUE;
$response["error_msg"] = "User does not exist!";
echo json_encode($response);
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters are missing!";
echo json_encode($response);
}
?>
我总是得到Required parameters are missing!
。有人可以帮我解决这个问题吗?
答案 0 :(得分:2)
不是 POST 。这是 GET 。我将您的代码分别更改为GET Method。
<?php
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
$response = array("error" => FALSE);
if (isset($_GET['email']) && isset($_GET['password'])) {
$email = $_GET['email'];
$password = $_GET['password'];
$user = $db->getUserByEmail($email, $password);
if ($user != FALSE) {
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["date_created"] = $user["date_created"];
echo json_encode($response);
} else {
$response["error"] = TRUE;
$response["error_msg"] = "User does not exist!";
echo json_encode($response);
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters are missing!";
echo json_encode($response);
}
?>
答案 1 :(得分:1)
http://192.168.2.15/login_api/login.php?email=test@test.com&password=test 等网址中的所有变量都是 GET 参数
Yoy正在通过GET方法发送数据