【问题标题】:How to send POST request to Phil Sturgeon's CodeIgniter RestServer如何向 Phil Sturgeon 的 CodeIgniter RestServer 发送 POST 请求
【发布时间】:2012-07-20 14:45:06
【问题描述】:

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

这是我在控制器中的 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 没有任何价值,因为我确实回显了 $result->status 并且它没有任何内容可显示。我在这个控制器的构造函数中也有这个:

// 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 中的教程并搜索了这个,但仍然......可能是因为我的英语阅读能力不好。我希望有人可以帮助我,任何帮助将不胜感激。万分感谢! :)

【问题讨论】:

    标签: codeigniter rest rest-client http-post


    【解决方案1】:

    终于解决了这个问题! 我在 RESTClient 控制器中的 POST 请求是错误的。在进行了一些搜索和大量尝试/更改代码之后,此代码适用于我的 RESTClient 控制器中的 POST 请求:

    $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
    • cURL (philsturgeon) v.1.2.1
    • CodeIgniter v.2.0.3

    【讨论】:

      【解决方案2】:

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

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

      基本上你在 RestServer application/libraries/REST_Controller.php 中找到 post 函数,如果它看起来不像下面那样,则将其更改为:

      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;
      }
      

      【讨论】:

      • REST_Controller.php 中的 post 函数看起来确实和你在那里写的一模一样,我在 2-3 周前在 github 上下载了 CI RestServer,所以它可能仍然是最新的源。你还有什么建议吗?还是其他人?谢谢.. :)
      猜你喜欢
      • 2023-03-19
      • 2013-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多