我遇到类似以下错误:尝试使用邮递员发布数据时,尝试在第24行的D:\ ShiroWorks \ rest-api-authentication-example \ api \ create_user.php中获取非对象的属性“名字”。
我尝试使用file_get_contents()函数作为数组,但收到消息“无法创建用户。”
header("Access-Control-Allow-Origin: http://localhost/rest-api-authentication-example/");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
include_once 'config/database.php';
include_once 'objects/user.php';
$database = new Database();
$db = $database->getConnection();
$user = new User($db);
$data = json_decode(file_get_contents("php://input"));
$user->firstname = $data->firstname;
$user->lastname = $data->lastname;
$user->email = $data->email;
$user->password = $data->password;
// create the user
if(
!empty($user->firstname) &&
!empty($user->email) &&
!empty($user->password) &&
$user->create()
){
http_response_code(200);
// display message: user was created
echo json_encode(array("message" => "User was created."));
}
// message if unable to create user
else{
http_response_code(400);
// display message: unable to create user
echo json_encode(array("message" => "Unable to create user."));
}
?>
// The result of var_dump($data) is:
array(4) {
[
"firstname"
]=>
string(6) "Vilmos"
[
"lastname"
]=>
string(5) "Szabó"
[
"email"
]=>
string(15) "shiro@email.com"
[
"password"
]=>
string(3) "555"
}
我应该找回“消息”:“用户已创建。”
答案 0 :(得分:1)
该错误是由于数据库所致。另一行被设置为NOT NULL,我没有将其包含在发布请求中。