gpt4 book ai didi

codeigniter - 如何向 Phil Sturgeon 的 CodeIgniter RestServer 发送 POST 请求

转载 作者:行者123 更新时间:2023-12-04 19:14:22 26 4
gpt4 key购买 nike

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

这是我在 Controller 中的 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 没有任何值(value),因为我确实回应了 $result->状态它没有什么可显示的。我在这个 Controller 的构造函数中也有这个:
// 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 的 Controller 中,它是 新闻 Controller ,有这个方法:
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 Controller 中的 POST 请求是错误的。在进行了一些搜索和大量尝试/更改代码之后,此代码适用于我的 RESTClient Controller 中的 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
  • 关于codeigniter - 如何向 Phil Sturgeon 的 CodeIgniter RestServer 发送 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11581624/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com