【问题标题】:Webservice for mobile Application using REST Service in PHP在 PHP 中使用 REST 服务的移动应用程序的 Web 服务
【发布时间】:2012-08-24 10:21:03
【问题描述】:

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

我使用以下链接开发REST服务

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

我可以通过 html 中的表单添加新数据,但我想使用 url 添加数据。但我不能。这是我使用的代码

<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>

当我尝试通过此表单时,操作已成功完成。但我想把它作为网络服务。我尝试通过 url 添加数据但失败,同时使用连接查询删除或获取数据也不起作用。

我使用以下函数从 Db 检索数据

$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"
            ));
    }
});

这也很好用。

是否有任何其他方式或好的样品可用。不仅在 Slim 中。我需要集成 REST 服务。请就此提出建议。

提前致谢。

【问题讨论】:

  • “我可以通过 html 中的表单添加新数据但我想使用 url 添加数据”到底是什么意思?你想开发带有 url 值的休息服务吗?
  • 我想开发带有 url 值的休息服务

标签: php web-services rest restful-architecture


【解决方案1】:

好的,我认为这是您需要开始的地方:)

如果您要使用 url 来设置数据,那么您必须使用 HTTP get 方法。如果你使用java开发restful服务我建议你使用Jersey(JAX-RS (JSR 311) Reference Implementation for build RESTful Web services.)

在您的项目服务中,您可以使用 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/”是什么,您都可以使用该值。

如果您想开始使用 RESTful 服务,使用 Netbeans 非常容易。 以下是一些可能对您有所帮助的链接。

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

【讨论】:

  • 感谢 RJ45。我认为它是基于 Java 的。我只需要 PHP。可以在 PHP 中使用它吗?
  • Restful 是一种架构,所以它不依赖于一种编程语言。但是在 php 上很难找到好的例子。同样如这里 (en.wikipedia.org/wiki/…) 所述,如果我们可以根据相关标准开发服务,那么它就是一项宁静的服务。
猜你喜欢
  • 1970-01-01
  • 2016-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多