[英]Rest API Web Service - iOS
Its my first time to use web service in iOS. 这是我第一次在iOS中使用网络服务。 REST was my first choice and I use code igniter to form it.
REST是我的首选,我使用代码点火器来形成它。
I have my Controller: 我有我的控制器:
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);
}
}
, using my web browser, accessing the URL http://localhost:8888/restApi/index.php/sample/example/format/json
really works fine and gives this output: ,使用我的网络浏览器访问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"}]}
, this gives me a great output using RKRequest by RestKit in my app. ,这在我的应用程序中使用RestKit的RKRequest给了我很大的输出。
The problem goes with the put method. 问题在于put方法。 This URL :
这个网址:
http://localhost:8888/restApi/index.php/sample/user
always give me an error like this: 总是给我这样的错误:
This XML file does not appear to have any style information associated with it. 此XML文件似乎没有与之关联的任何样式信息。 The document tree is shown below.
文档树如下所示。
<xml>
<status/>
<error>Unknown method.</error>
This is my Users model 这是我的用户模型
<?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);
}
}
?>
What is the work around for my _put method why am I not inserting anything? 我的_put方法的解决方法是什么,为什么我什么都不插入? Thanks all!
谢谢大家!
Unless you set the method to PUT
or POST
, your web server is not going to treat it as such. 除非将方法设置为
PUT
或POST
,否则您的Web服务器不会将其视为此类。 When you enter URLs in a browser bar, that is almost always a GET request. 在浏览器栏中输入URL时,几乎总是GET请求。 You might try to use
curl
like 您可以尝试使用
curl
curl -X POST -d @filename http://your.url.path/whatever
Another link would be: https://superuser.com/questions/149329/what-is-the-curl-command-line-syntax-to-do-a-post-request 另一个链接是: https : //superuser.com/questions/149329/what-is-the-curl-command-line-syntax-to-do-a-post-request
So you should be able to do a PUT similarly (perhaps with no data). 因此,您应该能够类似地执行PUT(也许没有数据)。 Not really sure if this should be iOS tagged though :)
虽然不太确定这是否应该用iOS标记:)
I got this problem by using Firefox plug in rest Client. 我通过在REST Client中使用Firefox插件来解决此问题。 I just have to indicate the headers and the body to make put and post work.
我只需要指出标题和正文即可进行放置和发布工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.