Poles
Poles

Reputation: 3682

How to recieve post json data on PHP server page

I have the following json body which I'm posting from an app through API.

{
  "GetUserMasterOperation" : "GetUserMasterOperation",
  "do" : "INSERT",
  "Email" : "[email protected]",
  "UserName" : "test",
  "Password" : "123",
  "MobileId" : "BB767CA0"
} 

On the server side I used this PHP code to retrive the json in 'webservices.php':

<?php
include_once("lib/api_class.php");
$api= new API();

if($api->get_request_method()=='POST' && isset($_POST['GetUserMasterOperation']))
{
    header('Content-type: application/json');
    $api->UserMasterOperation($_POST);
}
?>

now the 'api_class.php':

<?php
require_once "dbconnect.php";
class API extends dbconnect
{           
    public function __construct()
    {
        $db=new dbconnect;
        $db->connect();
    }

    public function UserMasterOperation($postJson)
    {       
        $post=json_decode($postJson,true);  

        if($post['do']=='INSERT')
        {            
              $status = false;
              $dt= $this->sanitate($post);
              $MobileId=$dt['MobileId'];
              $WearableId=$dt['WearableId'];
              $UserName=$dt['UserName'];
              $Email=$dt['Email'];
              $pass=md5(trim($dt['Password']));

              $sql="INSERT INTO `usermaster` SET `MobileId`='".$MobileId."', `WearableId`='".$WearableId."',`UserName`='".$UserName."', `Email`='".$Email."', `Password`='".$pass."', `Status`='Active', `Deleted`='no', `CrDate`=NOW()";

              if(mysql_query($sql))
              { 
                $last_id=mysql_insert_id();    
                  if(mysql_affected_rows()==1)
                  {                     
                    $result='Your account has been created.';
                    $response=array('stauscode'=>200,'msg'=>$result, 'userID'=>$last_id);

                    return $this->response($this->json($response), 200);
                  }
                  else
                  {
                    return $this->response($this->json(array('stauscode'=>400,'msg'=>'Bad request')), 400);            
                  }
              }         
        }    

        else
        {
            return $this->response($this->json(array('stauscode'=>800,'msg'=>'Missing Parameters')), 800);            
        }
    }
}
?>

But in return I got nothing. Is there any problem in receiving the posted json data? Please help.

Upvotes: 0

Views: 161

Answers (2)

Poles
Poles

Reputation: 3682

At last with the help of you guys, I figured it out myself. The webservices.php should be like this:

<?php
include_once("lib/api_class.php");
$api= new API();
header('Content-type: application/json'); 
$input=json_decode(file_get_contents('php://input'),TRUE);

if($api->get_request_method()=='POST' && isset($input['GetUserMasterOperation']))
{

    $api->UserMasterOperation($input);
}

?>

And that's all. you will get the whole json data on server side page.

Upvotes: 0

Prasanth
Prasanth

Reputation: 5258

You might have raw_post_data.

You can read this data from php://input stream or set your always_populate_raw_post_data configuration variable to true in your php.ini file to have the raw data populated to $_POST variable always.

Upvotes: 1

Related Questions