0

I have been attempting to send my data from my app to my local server via php. I am able to parse my data through and serialised it into JSON and supposedly send it to the server like this question will demonstrate. However when I test it on POSTMAN, the $array says that it is NULL and the $json says string(0)"". I am very confused as to why this is the case.

This is how my data is structured when parsed:

Optional([["record_id": 8EC9C1C9-7DD4-4343-B7CC-E4615FDDA150, "name": John ], ["record_id": 7EEA551D-9432-4737-99FB-6BFCF3A92D21, "name": Fred Smith]])

Below is my php file:

<?php
ini_set("display_errors", 1);
ini_set("html_errors", 1);
error_reporting(E_ALL);

$json = file_get_contents('php://input');
var_dump($json);

// convert to array
$array = json_decode($json, true);
var_dump($array);

$response = array();

//check if request is post
if($_SERVER['REQUEST_METHOD']=='POST'){



    //assign values to variables
    $recordID = $array['record_id'];
    $name = $array['name'];



    //including the db operation file
    require_once '../includes/DbOperation.php';

    $db = new DbOperation();

    //inserting values
    if($db->createTeam($recordID, $name)){
        $response['error']=false;
        $response['message']='Record added successfully';
    }else{

        $response['error']=true;
        $response['message']='Could not add record';
    }

}else{
    $response['error']=true;
    $response['message']='You are not authorized';
}
echo json_encode($response);

and here is my DBOperations class:

<?php

class DbOperation
{
    private $conn;

    //Constructor
    function __construct()
    {
        require_once dirname(__FILE__) . '/Config.php';
        require_once dirname(__FILE__) . '/DbConnect.php';
        // opening db connection
        $db = new DbConnect();
        $this->conn = $db->connect();
    }

    //Function to create a new user
    public function createTeam($recordID, $name)
    {
        $stmt = $this->conn->prepare('INSERT INTO record(record_id, name) VALUES (?, ?)');
        $stmt->bind_param("si", $recordID, $name);
        $result = $stmt->execute();
        $stmt->close();
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

}

I am really struggling to resolve this. Please let me know what is the best way to handle this.

Chace
  • 561
  • 10
  • 28
  • 1
    Can you plz post how you are testing in postman. I mean json structure , send type etc. – Purushottam zende Sep 27 '17 at 07:20
  • I am not sure how to show you that? Type is: `No Auth`. This is the url: `http://localhost:8888/api/createteam.php?name=tester123&record_id=hfdgjn3wme4` – Chace Sep 27 '17 at 07:22
  • Method -> propfind, URL -> http://localhost:8888/api/createteam.php?name=tester123&reco‌​rd_id=hfdgjn3wme4, data to send (body-> raw) -> {"username":"jnkjnk","password":"2344"}, i use like this. plz confirm. – Purushottam zende Sep 27 '17 at 07:27
  • The URL which I mentioned is a POST type, with the parameters to send. I am not sure what is it you want. Are you able to send a JSON array to a server? – Chace Sep 27 '17 at 08:15
  • Yes i was able to receive data on server, using above information. I mean i used same. – Purushottam zende Sep 27 '17 at 08:24
  • Can you please explain your method more clearer, it is very confusing at the moment. Where are you adding `{"username":"jnkjnk","password":"2344"}` in POSTMAN? – Chace Sep 27 '17 at 08:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/155402/discussion-between-purushottam-zende-and-chace). – Purushottam zende Sep 27 '17 at 08:44

0 Answers0