[英]Webservice for mobile Application using REST Service in PHP
我必須在Rest Service中為移動應用程序(iPhone和Android)創建Web服務。 該應用程序基於一種電子出版。 我嘗試一些基於SLIM的REST服務。 我可以向db中添加數據,也可以從DB中檢索數據。
我使用以下鏈接來開發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>
當我嘗試通過此表單時,操作成功完成。 但我想將其作為Web服務。 我嘗試通過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服務。 請對此提出建議。
提前致謝。
好的,我認為這是您需要開始的地方:)
如果要使用url設置數據,則必須使用HTTP get方法。 如果您使用Java開發靜態服務,則建議您使用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
}
}
因此,在您網址中的“ basepath /”之后,您都可以使用該值。
如果您想開始使用寧靜的服務,那么使用Netbeans非常容易。 這是一些可能對您有幫助的鏈接。
netbeans.org/kb/docs/websvc/rest.html
netbeans.org/kb/docs/websvc/intro-ws.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.