How to decode json data from post method for android app in php?

I am developing an application where I pass credentials and then pass it through the post method. On this page, I want to access the regno and pass value that were filled in on the login page. But I am not getting the value. I tried to iterate over values ​​like $ data-> regno but got Null value. Please tell me what I am doing wrong.

<?php

    ini_set('display_errors', '0');
    error_reporting(0);
    require_once("include/db.php");
    class mysendclass{
    public $name="Invalid";
    public $priority=-1;
    public $url="";
    }
    class myrecclass{
    public $regno="";
    public $pass="";
    }
    $e=new mysendclass();
    $g=new myrecclass();
    if(isset($_POST))
    {
        $data=json_decode(file_get_contents('php://input'));
        foreach ($data AS $key => $value) $g->{$key} = $value;
        $query = "SELECT priority, name, url FROM users WHERE regno='{$data->regno}' AND pass='{$data->pass}' LIMIT 1";
        echo ($query);
        $res = mysql_query($query, $conn) or die(mysql_error());
        $found = mysql_fetch_array($res);
        echo ($found);
        if($found)
        {
                $e->name=$found['name'];
                $e->priority=$found['priority'];
                $e->url=$found['url'];//url

            }
            echo json_encode($e);
    }

?>

      

When I submit the credentials, I get this despite posting the correct credentials from the database:

{"name":"Invalid","priority":-1,"url":""}

      

+3


source to share


2 answers


use this.

<?php
$abc = array();
$abc['my_first_name'] = "Suresh";
$abc['my_last_name'] = "Kumar";
echo json_encode($abc);
?>

      

return valid json sting -



{"my_first_name":"Suresh","my_last_name":"Kumar"}

      

This is a simple example for your understanding. In your request, you are encoding an object, not an array, I'm not sure but I think this is your problem try this ...

<?php

    ini_set('display_errors', '0');
    error_reporting(0);
    require_once("include/db.php");
    class mysendclass{
    public $name="Invalid";
    public $priority=-1;
    public $url="";
    }
    class myrecclass{
    public $regno="";
    public $pass="";
    }
$records = array();
    $e=new mysendclass();
    $g=new myrecclass();
    if(isset($_POST))
    {
        $data=json_decode(file_get_contents('php://input'));
        foreach ($data AS $key => $value) $g->{$key} = $value;
        $query = "SELECT priority, name, url FROM users WHERE regno='{$data->regno}' AND pass='{$data->pass}' LIMIT 1";
        echo ($query);
        $res = mysql_query($query, $conn) or die(mysql_error());
        $found = mysql_fetch_array($res);
        echo ($found);
        if($found)
        {
                $records['name']=$found['name'];
                $records['priority']=$found['priority'];
                $records['url']=$found['url'];//url

            }
            echo json_encode($records);
    }

?>

      

+2


source


Use an array as a parameter in the json_encode () function instead of an object.



0


source







All Articles