1

Php web services with Android

 2 years ago
source link: https://www.codesd.com/item/php-web-services-with-android.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Php web services with Android

advertisements

I'm new to PHP programming and have poor knowledge about it, but I want to use it to make web services to my client Android application...

I began making my first web service with PHP and it's working fine, but I want to know how I can make one file that has all my functions and methods that I need and how to call it from Android

Thank you

This is functions.php

    <?php
   function GetAllRest()
    {
    mysql_connect("localhost","root","root");
    mysql_select_db("myhoteldb");
     $sql=mysql_query("SELECT rest_id ,rest_name,cuisine,no_tables,bg_img,rest_logo      FROM  restaurant");
  while($row=mysql_fetch_assoc($sql))
  $output[]=$row;
  print(json_encode($output));
  mysql_close();
}
function GetAllCategory($lang_id,$rest_id)
{
    mysql_connect("localhost","root","root");
    mysql_select_db("myhoteldb");
    $sql=mysql_query("SELECT cat_lang.rowid ,cat_lang.cat_id as _id,lang_id,   cat_name,cat_description from cat_lang  ,menu_category WHERE lang_id= $lang_id  AND  restaurant= $rest_id ");

      while($row=mysql_fetch_assoc($sql))
     $output[]=$row;
     print(json_encode($output));
      mysql_close();

  }

   ?>

and the URL http://localhost/mywebservices.php?op=GetAllCategory&lang_id=1&rest_id=1

and i got this error now

   Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in

C:\xampp\htdocs\functions.php on line 22

   Notice: Undefined variable: output in C:\xampp\htdocs\functions.php on line 24
    null


As an idea, I'd recommend including the function name desired to run in your url. And then, get that function name and arguments, pass them to php's

call_user_func_array()

function.

Here's a very basic idea of a function handler:

your request URL would look like this:

http://www.mysite.com/webservice.php?op=CallFunctionOne&param_a=test&param_b=test2

And here is the handler to route your calls to your functions:

require_once("./functions.php");

if(!empty($_SERVER["QUERY_STRING"])){
    $query_str = stripslashes($_SERVER['QUERY_STRING']);
    parse_str($query_str,$args);
    $op = array_shift($args);
    if(is_callable ($op)){
        call_user_func_array($op,$args);
    }
    else{
        echo "Handler error.<br />";
        echo $op . " function is not callable.";
    }
}

And functions.php file would include your functions. Hope it will give some idea.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK