Json解码尝试获取非对象的属性

时间:2019-03-17 17:19:40

标签: php json apache ubuntu apache2

我正在尝试制作自己的API,并且当我尝试解码发送到该API的json请求时,我会收到错误消息

这是我正在使用的代码:

$data = json_decode(utf8_encode(file_get_contents("php://input")));
$user->firstname = $data->firstname;
$user->lastname = $data->lastname;
$user->email = $data->email;
$user->password = $data->password;
$user->type = $data->type;

当我在xampp上对其进行本地测试时,此方法有效,但是当我将其推送至Ubuntu服务器时,则无效。添加json_last_error();

时返回错误4。

这是我得到的错误:

Trying to get property of non-object

这是我的json:

{ "firstname": "test", "lastname": "test", "email": "test@gmail.com", "password": "test", "type": 1 }

我正在使用ubuntu 18.04,php 7.2和apache2

预先感谢

1 个答案:

答案 0 :(得分:0)

确保您使用对我有用的https://而不是http:// ...!

希望这会有所帮助!