0

I'm trying to pass a json object through POST request on url but i get a php notice whenever i do so and an "empty" object is created instead and i get the following output:

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"}

This is the url i'm passing:

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;
    }

  }

Edit: It adds objects fine when using raw JSON objects on postman. I'm new to this, thanks in advance.

Amine
  • 583
  • 6
  • 21
  • It looks like `$capteur` isn't an object. Does a `Capteur` class even exist? And if so, can you post its code? – Major Productions Mar 10 '19 at 03:56
  • @MajorProductions Edited post! – Amine Mar 10 '19 at 04:01
  • btw @MajorProductions It adds objects fine when raw JSON objects on postman – Amine Mar 10 '19 at 04:12
  • 1
    You are sending URL parameters and not a post payload in the request body, see https://stackoverflow.com/questions/14551194/how-are-parameters-sent-in-an-http-post-request – Oli Mar 10 '19 at 08:20
  • @Oli So what do i do exactly to send data through URL? i'm sorry i'm quite not knowledgable – Amine Mar 10 '19 at 17:47
  • @Amine, no problem at all. You could use curl to do that for instance, `curl --data "code_capteur=20&etat=fonct&created_at&updated_at" http://localhost/php_rest_api/api/capteur/create.php` – Oli Mar 10 '19 at 20:02
  • Thanks a lot! @Oli – Amine Mar 10 '19 at 23:00

0 Answers0