5

I'm new to CodeIgniter. I'm using Phil Sturgeon's RestServer and RestClient. I've been trying to make a POST request in my CodeIgniter RestClient controller to update data in my CodeIgniter RestServer, but it never updates data in my database. I think my POST request is not right.

Here is my RestClient POST request in controller:

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

It seems that $result doesn't get any value, because I did echo the $result->status and it has nothing to display. And I've also have this in this controller's constructor :

// 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/'));

And in the RestServer's controller, which is newscontroller, has this method :

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

With the News_model :

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

I just don't know where I'm doing wrong, because I still don't understand how the POST request and method work. I've read through the tutorial in Nettuts and search about this, but still.. maybe because of my bad English reading-writing. I hope someone can help me out, any help would be appreciated. Thanks a TON! :)

emwiguna
  • 101
  • 1
  • 1
  • 8

2 Answers2

5

Finally SOLVED this problem! It was my POST request in the RESTClient controller that is wrong. After doing some searching and lots of trying / changing the codes, this code works for me for POST request in my RESTClient controller :

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

With a lot of help from this reference

I post this if anybody needs an example of the right POST request in RESTClient and RESTServer, because I find it hard to look for an example for POST request in RESTClient-Server*** by Phil Sturgeon.

I'm using :

  • RESTServer (philsturgeon) v.2.6.0
  • RESTClient (philsturgeon) v.2.1.0
  • cURL (philsturgeon) v.1.2.1
  • CodeIgniter v.2.0.3
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
emwiguna
  • 101
  • 1
  • 1
  • 8
0

There was a problem with the way the post was implemented. see issue on github and another issue on github.

You can either patch your code, or get the latest sources.

Basically you find the post function in the RestServer application/libraries/REST_Controller.php and if it does not look like the following then change it to:

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;
}
Kinjal Dixit
  • 7,777
  • 2
  • 59
  • 68
  • The post function in the REST_Controller.php did look exactly like you wrote there, and I downloaded the CI RestServer on github like 2-3 weeks ago, so it might be still the latest source. Do you have any more suggestion? Or anyone else? Thanks.. :) – emwiguna Jul 23 '12 at 09:11