2012-04-12 194 views
1

它是我第一次在iOS中使用Web服務。 REST是我的第一選擇,我使用代碼點火器來形成它。Rest API Web服務 - iOS

我有我的控制器:

require APPPATH.'/libraries/REST_Controller.php'; 

class Sample extends REST_Controller 
{ 

    function example_get() 
    { 
     $this->load->helper('arh'); 
     $this->load->model('users'); 

     $users_array = array(); 
     $users = $this->users->get_all_users(); 

     foreach($users as $user){ 
      $new_array = array( 
           'id'=>$user->id , 
           'name'=>$user->name, 
           'age'=>$user->age, 
          ); 
      array_push($users_array, $new_array); 
     } 

     $data['users'] = $users_array;  
     if($data) 
     { 
      $this->response($data, 200); 
     } 
    } 

    function user_put() 
    { 
     $this->load->model('users'); 
     $this->users->insertAUser(); 

     $message = array('message' => 'ADDED!'); 
     $this->response($message, 200); 

    } 

} 

,使用網頁瀏覽器,訪問URL http://localhost:8888/restApi/index.php/sample/example/format/json確實工作正常,並給出了這樣的輸出:

{"users":[{"id":"1","name":"Porcopio","age":"99"},{"id":"2","name":"Name1","age":"24"},{"id":"3","name":"Porcopio","age":"99"},{"id":"4","name":"Porcopio","age":"99"},{"id":"5","name":"Luna","age":"99"}]} 

,這給了我使用RKRequest一個偉大的輸出通過RestKit在我的應用程序。

問題與put方法一致。此網址:

http://localhost:8888/restApi/index.php/sample/user 

總是給我這樣的錯誤:

此XML文件沒有出現有任何相關的樣式信息。文檔樹如下所示。

<xml> 
<status/> 
<error>Unknown method.</error> 

這是我的用戶模型

<?php 

    class Users extends CI_Model { 
     function __construct() 
     { 
      parent::__construct(); 
     } 

     function get_all_users() 
     { 
      $this->load->database(); 
      $query = $this->db->get('users'); 
      return $query->result(); 
     } 

     function insertAUser(){ 
      $this->load->database(); 
      $data = array('name'=> "Sample Name", 'age'=>"99"); 
      $this->db->insert('users', $data); 
     } 
    } 
?> 

什麼工作都是圍繞我_put方法,爲什麼我不插入任何內容? 謝謝大家!

回答

0

我通過使用Firefox插件rest客戶端得到了這個問題。 我只需要指明標題和正文以進行放置和發佈工作。

1

除非您將方法設置爲PUTPOST,否則您的Web服務器不會像這樣對待它。當您在瀏覽器欄中輸入網址時,幾乎總是一個GET請求。你可以嘗試使用curl

curl -X POST -d @filename http://your.url.path/whatever 

另一個鏈接是:https://superuser.com/questions/149329/what-is-the-curl-command-line-syntax-to-do-a-post-request

所以,你應該能夠做一個類似PUT(或許沒有數據)。不太確定這是否應該是iOS標記:)

+0

我真的很困惑,所以我標記了iOS。 http://stackoverflow.com/questions/10136971/rest-web-service-for-reskit – ruelluna 2012-04-13 08:37:36