0

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
1

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

can you explain more . with total example as i told you i have poor knowledge in php
and if i have functions that didn't take args what it will be
you can just pass an empty array. If I explain more, basically what I showed you is a simple call handler based on URL. "op" is the function name you want to call from your android application. Once you make a request, your php application will parse the url and get the function name. Based on the function name, it'll callback that function which would be located in your functions.php file.
i used your way but i got this error Handler error. GetAllCategory function is not callable.
Where is that function located?
|
1

If you know PHP then I assume you'll know HTML.

PhoneGap allows you to run apps on andriod using HTML, Javascript and CSS. http://phonegap.com/start

Using the Javascript you could make a page request to your web php file.

Here is a jQuery example

<script>
$.ajax({
  url: 'http://example.com/myfile.php',
  dataType: 'json',
  success: function(data) {
    alert(data.rest_name); // do whatever you want with the json response
  }
});
</script>

To enable the cross-domain you need to add this into the php files header (unsure if Apps need cross-domain validation tho)

header('Access-Control-Allow-Origin: *');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.