如何发送POST请求给Phil Sturgeon的CodeIgniter RestServer

问题描述:

我是CodeIgniter的新手。我正在使用Phil Sturgeon的RestServer和RestClient。我一直试图在我的CodeIgniter RestClient控制器中创建POST请求来更新我的CodeIgniter RestServer中的数据,但它从不更新数据库中的数据。我认为我的POST请求不正确。如何发送POST请求给Phil Sturgeon的CodeIgniter RestServer

这里是我的控制器RESTClient实现POST请求:

$result = $this->rest->post('newscontroller/news/format/json', 
      array('news_id' => $news_id, 
       'news_title' => $news_title, 
       'news_category' => $news_category), 
       'json'); 

if(isset($result->status) && $result->status == 'success') 
{ 
     $data['message'] ='News has been updated.'; 
     $this->load->view('otherpageview',$data); 
}  
else 
{ 
     $data['message'] ='Something has gone wrong'; 
     $this->load->view('otherpageview',$data); 
} 

似乎$导致没有得到任何价值,因为我没有附和$ result->状态和它没有任何关系显示。而且我也有这个在这个控制器的构造函数:

// Load the rest client spark 
$this->load->spark('restclient/2.1.0'); 

// Load the library 
$this->load->library('rest'); 

// Run some setup 
$this->rest->initialize(array('server' => 'http://api.therestserver.com/index.php/')); 

,并在RestServer的控制器,这是newscontroller,有以下方法:

function news_post() 
{ 
    $news=array(
     'news_id' => $this->post('news_id'), 
     'news_title' => $this->post('news_title'), 
     'news_category' => $this->post('news_category')); 

    $result = $this->News_model->UpdateNews($news); 

    if($result === FALSE) 
    { 
     $this->response(array('status' => 'failed')); 
    } 
    else 
    { 
     $this->response(array('status' => 'success')); 
    } 
} 

随着News_model

public function UpdateNews($news) 
{ 
    $this->db->where('news_id',$news->news_id); 
    $this->db->update('news',$news);   
} 

我只是不知道我在做什么错,因为我仍然不明白他的POST请求和方法工作。我已经阅读了Nettuts中的教程并搜索了这个内容,但仍然可能是因为我的英文读写不好。我希望有人能帮助我,任何帮助将不胜感激。万分感谢! :)

终于解决了这个问题! 这是我在RESTClient控制器中的POST请求是错误的。做一些搜索和大量的努力/更改代码后,该代码工作对我来说POST请求我的RESTClient实现控制器:

$news_id = 12; //this is the id of the news that you want to edit 

$method = 'post'; 
$params = array('news_id' => $news_id, 
'news_title' => $this->input->post('news_title'), 
'news_category' => $this->input->post('news_category')); 
$uri = 'newscontroller/news'; 

$this->rest->format('application/json'); 

$result = $this->rest->{$method}($uri, $params); 

if(isset($result->status) && $result->status == 'success') 
{ 
    $data['message'] ='News has been updated.'; 
    $this->load->view('otherpageview',$data); 
}  
else 
{ 
    $data['message'] ='Something has gone wrong'; 
    $this->load->view('otherpageview',$data); 
} 

有了很大的帮助,从这个reference

如果有人我张贴此需要在RESTClient和RESTServer中提供正确的POST请求示例,因为我发现Phil Sturgeon很难在RESTClient-Server ***中寻找POST请求的示例。

我使用:

  • RESTServer(philsturgeon)v.2.6.0
  • RESTClient实现(philsturgeon)v.2.1.0
  • 卷曲(philsturgeon)v.1.2.1
  • CodeIgniter v.2.0.3

该帖子的实施方式存在问题。见issue on githubanother issue on github

您可以修补您的代码或获取最新的来源。

基本上你找到邮局功能在RestServer application/libraries/REST_Controller.php,如果它看起来并不像下面然后将其更改为:

public function post($key = NULL, $xss_clean = TRUE) 
{ 
    if ($key === NULL) 
    { 
     return $this->_post_args; 
    } 

    return array_key_exists($key, $this->_post_args) ? $this->_xss_clean($this->_post_args[$key], $xss_clean) : FALSE; 
} 
+0

REST_Controller中的post函数。php看起来和你在那里写的完全一样,并且我在2-3周前在github上下载了CI RestServer,所以它可能仍然是最新的源代码。你有什么建议吗?还是其他人?谢谢.. :) – emwiguna 2012-07-23 09:11:34