2012-04-12 149 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