0

私はPHPプログラミングに不慣れで、それについての知識が乏しいですが、それを使用してクライアントのAndroidアプリケーションへのWebサービスを作成したいと思います...

PHPを使用して最初のWebサービスを作成し始めましたが、正常に機能していますが、必要なすべての関数とメソッドを含む1つのファイルを作成する方法と、Androidから呼び出す方法を知りたいです。

ありがとうございました

これは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();

  }



   ?>

および URLhttp://localhost/mywebservices.php?op = GetAllCategory&lang_id = 1&rest_id = 1

そして私は今このエラーを受け取りました

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

22行目のC:\ xampp \ htdocs \ features.php

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

2 に答える 2

1

アイデアとして、実行したい関数名を URL に含めることをお勧めします。そして、その関数名と引数を取得し、それらをphpのに渡します

call_user_func_array()

関数。

関数ハンドラの非常に基本的な考え方は次のとおりです。

リクエスト URL は次のようになります。

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

呼び出しを関数にルーティングするハンドラーは次のとおりです。

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.";
    }
}

functions.php ファイルには関数が含まれます。それがいくつかのアイデアを与えることを願っています。

于 2012-04-09T07:35:19.447 に答える