2012-08-24 88 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

相關問題