2012-08-24 54 views
0

我必须在Rest服务中为移动应用程序(iPhone和Android)创建一个Web服务。此应用程序基于一个电子出版。我尝试一些基于SLIM的REST服务。我可以将数据添加到数据库并从数据库中检索数据。在PHP中使用REST服务的移动应用程序Webservice

我使用下面的链接来开发REST服务

http://phpmaster.com/writing-a-restful-web-service-with-slim/ 

我能够通过HTML表单添加新的数据,但我想使用的网址添加数据。但我不能。这里是我使用的代码

<form action="http://localhost/samples/Restfull/samp5/index.php/custom" method="POST"> 
<input type="hidden" name="_METHOD" value="POST"> 
Name: <input type="text" name="Customer_Name"><br> 
Mobile: <input type="text" name="Customer_Mobile"><br> 
Email: <input type="text" name="Customer_Email"><br> 
Address: <textarea name="Customer_Address"></textarea> 
<br> 
<input type="submit" value="Submit"> 
</form> 

当我尝试通过这种形式,操作成功完成。但是我想把它作为Web服务。我尝试通过url添加数据但失败,同时使用连接查询删除或获取数据也无效。

我用下面的函数

$app->get("/custom/:id", function ($id) use ($app, $db) { 
    $app->response()->header("Content-Type", "application/json"); 
    $custom = $db->Registration()->where("Registration_Id", $id); 
    if ($data = $custom->fetch()) { 
     echo json_encode(array(
      "custom_Name" => $data["Customer_Name"], 
      "custom_Mobile" => $data["Customer_Mobile"], 
      "custom_Email" => $data["Customer_Email"], 
      "custom_Address" => $data["Customer_Address"] 
      )); 
    } 
    else{ 
     echo json_encode(array(
      "status" => false, 
      "message" => " $id does not exist" 
      )); 
    } 
}); 

这也是行之有效的从数据库取回数据。

是否有其他方式或好样品可用。不仅在苗条。我需要集成REST服务。请在此建议。

在此先感谢。

+0

你究竟是“我能够通过形式在HTML中添加新的数据,但我想使用的网址添加数据”是什么意思?你想开发其余的服务,它需要url的值? – Ruwantha

+0

S我想开发其余的服务,它采取值与网址 –

回答

0

好吧,我觉得这就是你需要开始:)

如果你要使用的URL来设置数据,那么你必须使用HTTP GET方法。如果你使用的Java开发RESTful服务,我建议你使用Jersey(JAX-RS(JSR 311)参考实现用于构建RESTful Web服务。)

在你的项目的服务,您可以定义HTTP GET方法

方法
@Stateless 
@Path("/basepath") 
@javax.ws.rs.Produces("application/json") 
@javax.ws.rs.Consume("application/json") 
public class RestService { 

    @Path("/{index}") 
    public String M(@PathParam("index") String index){ 
     //you can use index value here 

    } 

} 

因此,在URL中的“basepath /”之后,您可以使用该值。

如果您想开始使用宁静的服务,那么使用Netbeans很容易。 这里有一些链接可能会帮助你。

netbeans.org/kb/docs/websvc/rest.html
netbeans.org/kb/docs/websvc/intro-ws.html

+0

谢谢RJ45。我认为这是基于Java ..我纯粹需要PHP。有可能在PHP中使用这个? –

+0

Restful是一种体系结构,因此它不依赖于编程语言。但很难在php上找到好的示例。同样如此处所述(http://en.wikipedia.org/wiki/Representational_state_transfer#RESTful_web_services)如果我们可以将服务开发到相关标准,那么这是一项宁静的服务。 – Ruwantha

相关问题