[英]php webservices with android
我是 PHP 编程的新手并且对此知之甚少,但我想用它为我的客户 Android 应用程序提供 web 服务......
我开始用 PHP 制作我的第一个 web 服务,它工作正常,但我想知道如何制作一个包含我需要的所有函数和方法的文件,以及如何从 Android 调用它
谢谢
这是函数.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();
}
?>
和 URL http://localhost/mywebservices.php?op=GetAllCategory&lang_id=1&rest_id=1
我现在收到这个错误
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in
C:\xampp\htdocs\functions.php 第 22 行
Notice: Undefined variable: output in C:\xampp\htdocs\functions.php on line 24
null
作为一个想法,我建议包括希望在您的 url 中运行的 function 名称。然后,获取 function 名称和 arguments,将它们传递给 php 的
call_user_func_array()
function。
这是 function 处理程序的一个非常基本的概念:
您的请求 URL 将如下所示:
http://www.mysite.com/webservice.php?op=CallFunctionOne¶m_a=test¶m_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 文件将包含您的函数。 希望它能给出一些想法。
如果您知道 PHP,那么我假设您会知道 HTML。
PhoneGap 允许您使用 HTML、Javascript 和 CSS 在 andriod 上运行应用程序。http://phonegap.com/start
使用 Javascript,您可以向 web php 文件发出页面请求。
这是一个 jQuery 的例子
<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>
要启用跨域,您需要将其添加到 php 文件 header(不确定应用程序是否需要跨域验证)
header('Access-Control-Allow-Origin: *');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.