Ashish Baboo
Ashish Baboo

Reputation: 154

POST data is not being received in API call

I have built an API on PHP. While sending a POST request, variables provided in POST url are not being received and input data-set is empty. data variable in below provided create.php is empty.

If we supply hard coded data in create PHP file, then it is working fine.

Below is my main data.php file code. Which contains the function for creating the product using POST variables.

<?php
class Data{
private $conn;
private $table_name = "data";
public $id;
public $email;
public $address;
public $lasttx;
public $created;
public function __construct($db){
$this->conn = $db;
}

function create(){
$query = "INSERT INTO " . $this->table_name . " SET email=:email, 
address=:address, lasttx=:lasttx, created=:created";

$stmt = $this->conn->prepare($query);

$this->email=htmlspecialchars(strip_tags($this->email));
$this->address=htmlspecialchars(strip_tags($this->address));
$this->lasttx=htmlspecialchars(strip_tags($this->lasttx));
$this->created=htmlspecialchars(strip_tags($this->created));

$stmt->bindParam(":email", $this->email);
$stmt->bindParam(":address", $this->address);
$stmt->bindParam(":lasttx", $this->lasttx);
$stmt->bindParam(":created", $this->created);

if($stmt->execute()){
return true;
  }
return false;
}

Below is the code of create.php which is being called in API calls. This file receives data and calls create function in data.php

<?php
header("Access-Control-Allow-Origin: *");
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/data.php';

$database = new Database();
$db = $database->getConnection();

$data1 = new Data($db);

$data = json_decode(file_get_contents("php://input"), true); // THIS VARIABLE is EMPTY while calling API.

$data1->email = $data["email"];
$data1->address = $data["address"];
$data1->lasttx = $data["lasttx"];
$data1->created = date('Y-m-d H:i:s');

if($data1->create()) {
    echo '{';
        echo '"message": "Product was created."';
    echo '}';
}
else{
    echo '{';
        echo '"message": "Unable to create product."';
    echo '}';
}
?>

Please Advise. Many thanks.

Upvotes: 0

Views: 705

Answers (1)

Matthias
Matthias

Reputation: 2775

u as you point out in your comment the form variables are submitted via the URL. In your above script, you're trying to parse a JSON object that is the payload of the request. And that's just not where these parameters are - hence you don't get any data from there.

To access variables from the URL in PHP, all you need to do is to get them from the $_GET array. e.g. the email would be in $_GET["email"].

so this part here:

$data = json_decode(file_get_contents("php://input"), true); // THIS VARIABLE is EMPTY while calling API.
$data1->email = $data["email"];
$data1->address = $data["address"];
$data1->lasttx = $data["lasttx"];
$data1->created = date('Y-m-d H:i:s');

will become:

# $data = json_decode line goes away
$data1->email = $_GET["email"];
$data1->address = $_GET["address"];
$data1->lasttx = $_GET["lasttx"];
$data1->created = date('Y-m-d H:i:s');

hope this helps

Matthias

Upvotes: 1

Related Questions