简体   繁体   中英

respond to http get request with json object in php

I have a PHP page (test.php) that has to respond to a HTTP GET request sent from an android application. The response has to be via a json object. How do I do that?

For reference :- Below is a php code that checks the login details from a login form. The http request would be the username and password sent from an android application that contains the login form. The response from this page would be the success or failure message.

<?php
        session_start();
         require('connect.php');
        if (isset($_GET['username']) and isset($_GET['password']))
        {
            $username = $_GET['username'];
            $password = $_GET['password'];
            $query = "SELECT * FROM `staff_reg` WHERE username='$username' and password='$password'";

            $result = mysql_query($query) or die(mysql_error());
            $count = mysql_num_rows($result);
            if ($count == 1)
            {
                $_SESSION['username'] = $username;
            }
            else
            {
                echo "Invalid Username or Password";
            }
        }
        if (!isset($_SESSION['username']))
        {
            echo "Session Expired";
            echo "<br><a href='staff_login.php'>Click here</a> to login again"; 
            echo "<br><a href='index.html'>Click here</a> to go back to the homepage"; 
            $username = $_SESSION['username'];
        }
        else{
            $username = $_SESSION['username'];
            echo "<p>Welcome ".$username."<br>";
            echo "This is the Staff Portal<br>";
            echo "<a href='logout.php'>Logout</a></p>"; 
    ?>

I think it may help you..

<?php
        session_start();
         require('connect.php');
        //create an variable $response
        //Use GET instead of POST since you are sending data in GET method
        if (isset($_GET['username']) and isset($_GET['password']))
        {
            $username = $_GET['username'];
            $password = $_GET['password'];
            $query = "SELECT * FROM `staff_reg` WHERE username='$username' and password='$password'";

            $result = mysql_query($query) or die(mysql_error());
            $count = mysql_num_rows($result);
            if ($count == 1)
            {
                $response="Success";
            }
            else
            {
                $response="Failure";
            }
        //now echo it as an json object
            echo json_encode($response);
        }

You can send your response as json encoded data eg lets say your response data is

$user = array('firstName' =>'abc', 'lastName' =>'xyz'); 
$response = json_encode($user);

If this is an api for the android app you mentioned then you cannot use sessions in it. You have to find another way for authorizing the user.

You can encode your strings and arrays in json format but you'd better make an object and encode it. That way you can access the values by the name you specified in the api.

For example:

$obj->status = "error";

Or

$obj->status = "success";

And in the end:

echo json_encode($obj);

And you can access it in the application with this format:

   //String response = httpResponse
   JsonObject object = new JsonObject(response);
   String status = object.getString("status");
   if(status.equals("error")){
        //handle the error
   } else {
        //login was successful and you can show the user the proper activity
   }

As for sending the request to the api i recommend you to use the library AQuery .

The library can simplify your work with http requests. it works as follows:

    AQuery aq = new AQuery(context);
    aq.ajax(url, JsonObject.class, new AjaxCallback<JsonObject>(){
    @Override
    public void callback(String url, JSONObject object, AjaxStatus status) {
        //object is the json object sent from the server
    }
    });

I hope this helps you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM