我正在尝试通过url上的POST
请求传递一个json对象,但是每当我这样做时都会收到php
通知,而是创建了一个“空”对象,并且得到以下输出:
Notice: Trying to get property 'code_capteur' of non-object in C:\xampp\htdocs\php_rest_api\api\capteur\create.php on line 16
Notice: Trying to get property 'etat' of non-object in C:\xampp\htdocs\php_rest_api\api\capteur\create.php on line 17
Notice: Trying to get property 'created_at' of non-object in C:\xampp\htdocs\php_rest_api\api\capteur\create.php on line 18
Notice: Trying to get property 'updated_at' of non-object in C:\xampp\htdocs\php_rest_api\api\capteur\create.php on line 19
{"message":"capteur ajout\u00e9"}
这是我传递的网址:
http://localhost/php_rest_api/api/capteur/create.php?code_capteur=20&etat=fonct&created_at&updated_at
Create.php
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
include_once '../../config/Database.php';
include_once '../../models/Capteur.php';
$database = new Database();
$db = $database->connect();
$capteur = new Capteur($db);
$data = json_decode(file_get_contents("php://input"));
$capteur->code_capteur = $data->code_capteur;
$capteur->etat = $data->etat;
$capteur->created_at = $data->created_at;
$capteur->updated_at = $data->updated_at;
if($capteur->create()) {
echo json_encode(
array('message' => 'capteur ajouté')
);
} else {
echo json_encode(
array('message' => 'capteur pas ajouté')
);
}
Capteur.php
<?php
class Capteur {
private $conn;
private $table = 'capteurs';
public $id;
public $code_capteur;
public $etat;
public $etab;
public $created_at;
public $updated_at;
public function __construct($db) {
$this->conn = $db;
}
public function read() {
$query = 'SELECT * FROM ' . $this->table . '
ORDER BY
created_at DESC';
$stmt = $this->conn->prepare($query);
$stmt->execute();
return $stmt;
}
public function read_single() {
$query = 'SELECT * FROM ' . $this->table . '
where id = ?
LIMIT 0,1';
$stmt = $this->conn->prepare($query);
$stmt->bindParam(1, $this->id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$this->id = $row['id'];
$this->code_capteur = $row['code_capteur'];
$this->etat = $row['etat'];
$this->etab = $row['etab'];
$this->created_at = $row['created_at'];
$this->updated_at = $row['updated_at'];
}
public function create() {
$query = 'INSERT INTO ' . $this->table . ' SET code_capteur = :code_capteur, etat = :etat, created_at = :created_at, updated_at = :updated_at';
$stmt = $this->conn->prepare($query);
$this->code_capteur = htmlspecialchars(strip_tags($this->code_capteur));
$this->etat = htmlspecialchars(strip_tags($this->etat));
$this->created_at = htmlspecialchars(strip_tags($this->created_at));
$this->updated_at = htmlspecialchars(strip_tags($this->updated_at));
$stmt->bindParam(':code_capteur', $this->code_capteur);
$stmt->bindParam(':etat', $this->etat);
$stmt->bindParam(':created_at', $this->created_at);
$stmt->bindParam(':updated_at', $this->updated_at);
if($stmt->execute()) {
return true;
}
printf("Error: %s.\n", $stmt->error);
return false;
}
public function update() {
$query = 'UPDATE ' . $this->table . '
SET code_capteur = :code_capteur, etat = :etat, created_at = :created_at, updated_at = :updated_at
WHERE id = :id';
$stmt = $this->conn->prepare($query);
$this->code_capteur = htmlspecialchars(strip_tags($this->code_capteur));
$this->etat = htmlspecialchars(strip_tags($this->etat));
$this->created_at = htmlspecialchars(strip_tags($this->created_at));
$this->updated_at = htmlspecialchars(strip_tags($this->updated_at));
$this->id = htmlspecialchars(strip_tags($this->id));
$stmt->bindParam(':code_capteur', $this->code_capteur);
$stmt->bindParam(':etat', $this->etat);
$stmt->bindParam(':created_at', $this->created_at);
$stmt->bindParam(':updated_at', $this->updated_at);
$stmt->bindParam(':id', $this->id);
if($stmt->execute()) {
return true;
}
printf("Error: %s.\n", $stmt->error);
return false;
}
public function delete() {
$query = 'DELETE FROM ' . $this->table . ' WHERE id = :id';
$stmt = $this->conn->prepare($query);
$this->id = htmlspecialchars(strip_tags($this->id));
$stmt->bindParam(':id', $this->id);
if($stmt->execute()) {
return true;
}
printf("Error: %s.\n", $stmt->error);
return false;
}
}
编辑:在邮递员上使用原始JSON
对象时,可以很好地添加对象。
我是新来的,谢谢。