【问题标题】:Trying to get property of non-object error while I should get back an object?试图获取非对象错误的属性,而我应该取回一个对象?
【发布时间】:2019-12-04 00:41:22
【问题描述】:

我收到如下错误:尝试使用邮递员发布数据时,尝试在第 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"
}

我应该取回“消息”:“用户已创建。”

【问题讨论】:

  • 我们需要邮递员数据来回答这个问题。在邮递员中,当您在此请求的选项卡上时,单击发送按钮下方的代码超链接(cookie 和 cmets 旁边)。从下拉列表中选择 CURL 并将该代码粘贴到此处作为问题的一部分。您的 PHP 代码看起来不错,所以我怀疑您的请求格式不正确。
  • json_decode 默认返回对象,但您的 var_dump 显示类型为 array。它是怎么发生的?)
  • 你能在评论// create the user之前运行var_dump($user);吗?
  • ``` object(User)#3 (7) { [ "conn": "User":private ]=> object(PDO)#2 (0) {} [ "table_name": "User":private ]=> string(5) "users" [ "id" ]=> NULL [ "firstname" ]=> string(6) "Vilmos" [ "lastname" ]=> string(6) "Szabó " [ "电子邮件" ]=> 字符串(15) "shiro@email.com" [ "密码" ]=> 字符串(3) "555" } ```
  • Postman 数据太长,分两部分发。 ``` curl -X POST \ localhost:8888/api/create_user.php \ -H '接受:/' \ -H '接受编码:gzip,放气' \ -H '缓存控制:无缓存' \ -H '连接:保持活动' \ -H '内容长度:119' \ -H '内容类型:文本/纯文本' \ -H 'Cookie:PHPSESSID=97hhkrlai3apbcg3g2h9rcj5i7' \ -H '主机:本地主机: 8888' \ -H '邮递员令牌:bbb45f60-9e7b-4b7d-989b-fe864a872eca,b0b9ffc7-4d75-4fa6-9e11-eee99d21f896' \ ```

标签: php rest postman


【解决方案1】:

错误是因为数据库。另一行设置为 NOT NULL,我没有将其包含在发布请求中。

【讨论】:

  • 我在同一个教程中发现了同样的问题,并且可以确认 SQL 字段“created”和“modified”在创建数据库表时需要默认值为 null。抛出该错误是因为将用户写入用户表的 SQL 语句中未包含日期字段。只需将表日期字段设置为允许 NULL 值即可解决问题。
猜你喜欢
  • 2018-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-08
  • 2016-06-13
  • 2018-06-02
  • 2017-08-20
相关资源
最近更新 更多