emw*_*una 5 rest codeigniter http-post rest-client
我是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);
}
Run Code Online (Sandbox Code Playgroud)
似乎$ 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/'));
Run Code Online (Sandbox Code Playgroud)
而在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'));
}
}
Run Code Online (Sandbox Code Playgroud)
使用News_model:
public function UpdateNews($news)
{
$this->db->where('news_id',$news->news_id);
$this->db->update('news',$news);
}
Run Code Online (Sandbox Code Playgroud)
我只是不知道我在哪里做错了,因为我仍然不明白POST请求和方法是如何工作的.我已经阅读了Nettuts中的教程并搜索了这个,但仍然......也许是因为我的英语阅读写得不好.我希望有人可以帮助我,任何帮助将不胜感激.万分感谢!:)
终于解决了这个问题!这是我在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);
}
Run Code Online (Sandbox Code Playgroud)
从这个参考资料中获得了很多帮助
如果有人需要在RESTClient和RESTServer中使用正确的POST请求的示例,我发布此消息,因为我发现很难在PhilClurgeon的RESTClient-Server***中查找POST请求的示例.
我正在使用 :
| 归档时间: |
|
| 查看次数: |
12683 次 |
| 最近记录: |