phpjsontestingencodingposter

testing php function with poster extention


I made a simple API to get the data from the database to be viewd at android app

I have a Problem testing my php functions

example of the functions:-

<?php
include 'json_config.php';
function get_city(){
mysql_query("SET NAMES utf8;");
mysql_query("SET CHARACTER_SET utf8;");
$return=array();
//$country_name=$_POST['country_name'];

// get all products from products table
$result = mysql_query("SELECT *FROM city") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["city"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        // Full texts   id  dead_name   masged_name     gender  salah       notes   date    city_name   hour    min 
        $region_subscribt = array();
        $region_subscribt["id"] = $row["id"];
        $region_subscribt["city_name"] = $row["city_name"];
        $region_subscribt["region_name"] = $row["notes"];
        $region_subscribt["notes"] = $row["country_name"];
    // push single product into final response array
    array_push($response["city"], $region_subscribt);
}


// echoing JSON response
echo json_encode($response,JSON_UNESCAPED_UNICODE);
} else {
// no products found
$response["fail"] = 0;


// echo no users JSON
echo json_encode($response,JSON_UNESCAPED_UNICODE);
}
}//end of the function

// get_city();
?>

when I using poster extention with firefox and put at the content , I got no data when I put at the content

 getcity();

but when I call the function inside the php file I got the data correctly !

How to pass the values at the httprequest to use the returned json at another app !


Solution

  • You need to send the method name as a parameter not as a content and then check the method name and call the code you need.

    see this snapshot

    if(isset($_GET['method_name']) && $_GET['method_name']) == 'get_city') {
         get_city();
    }
    

    you also need to set the content type as following

    header('Content-type: application/json');
    

    your final code will be something like this

    <?php
    include 'json_config.php';
    function get_city(){
    mysql_query("SET NAMES utf8;");
    mysql_query("SET CHARACTER_SET utf8;");
    $return=array();
    //$country_name=$_POST['country_name'];
    
    // get all products from products table
    $result = mysql_query("SELECT *FROM city") or die(mysql_error());
    
    header('Content-type: application/json');
    
    // check for empty result
    if (mysql_num_rows($result) > 0) {
        // looping through all results
        // products node
        $response["city"] = array();
    
        while ($row = mysql_fetch_array($result)) {
            // temp user array
            // Full texts   id  dead_name   masged_name     gender  salah       notes   date    city_name   hour    min 
            $region_subscribt = array();
            $region_subscribt["id"] = $row["id"];
            $region_subscribt["city_name"] = $row["city_name"];
            $region_subscribt["region_name"] = $row["notes"];
            $region_subscribt["notes"] = $row["country_name"];
        // push single product into final response array
        array_push($response["city"], $region_subscribt);
    }
    
    
    // echoing JSON response
    echo json_encode($response,JSON_UNESCAPED_UNICODE);
    } else {
    // no products found
    $response["fail"] = 0;
    
    
    // echo no users JSON
    echo json_encode($response,JSON_UNESCAPED_UNICODE);
    }
    }//end of the function
    if(isset($_GET['method_name']) && $_GET['method_name']) == 'get_city') {
     get_city(); }
    ?>
    

    and here is some very good and pretty easy tutorial summarize the whole precess Create a Basic Web Service Using PHP, MySQL, XML, and JSON