这是我第一次在iOS中使用网络服务。 REST是我的第一选择,我使用代码点火器来形成它。
我有我的控制器:
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);
}
}
,使用我的网络浏览器,访问网址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"}]}
,这在我的应用程序中使用RestKit的RKRequest给了我很好的输出。
问题在于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 :(得分:1)
除非您将方法设置为PUT
或POST
,否则您的网络服务器不会将其视为此类。当您在浏览器栏中输入URL时,这几乎总是一个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标记:)
答案 1 :(得分:0)
我使用Firefox插件在rest Client中遇到了这个问题。 我只需要指出标题和正文以进行投入和后期工作。