get a PUT request with Codeigniter

28,213

Solution 1

According to: http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/ we should consult https://github.com/philsturgeon/codeigniter-restserver/blob/master/application/libraries/REST_Controller.php#L544 to see that this method:

/**
 * Detect method
 *
 * Detect which method (POST, PUT, GET, DELETE) is being used
 * 
 * @return string 
 */
protected function _detect_method() {
  $method = strtolower($this->input->server('REQUEST_METHOD'));

  if ($this->config->item('enable_emulate_request')) {
    if ($this->input->post('_method')) {
      $method = strtolower($this->input->post('_method'));
    } else if ($this->input->server('HTTP_X_HTTP_METHOD_OVERRIDE')) {
      $method = strtolower($this->input->server('HTTP_X_HTTP_METHOD_OVERRIDE'));
    }      
  }

  if (in_array($method, array('get', 'delete', 'post', 'put'))) {
    return $method;
  }

  return 'get';
}

looks to see if we've defined the HTTP header HTTP_X_HTTP_METHOD_OVERRIDE and it uses that in favor of the actual verb we've implemented on the web. To use this in a request you would specify the header X-HTTP-Method-Override: method (so X-HTTP-Method-Override: put) to generate a custom method override. Sometimes the framework expects X-HTTP-Method instead of X-HTTP-Method-Override so this varies by framework.

If you were doing such a request via jQuery, you would integrate this chunk into your ajax request:

beforeSend: function (XMLHttpRequest) {
   //Specify the HTTP method DELETE to perform a delete operation.
   XMLHttpRequest.setRequestHeader("X-HTTP-Method-Override", "DELETE");
}

Solution 2

You can try to detect the method type first and seperate the different cases. If your controller only handles REST functions it could be helpful to put get the required information in the constructor.

switch($_SERVER['REQUEST_METHOD']){
   case 'GET':
      $var_array=$this->input->get();
      ...
      break;
   case 'POST':
      $var_array=$this->input->post();
      ...
      break;
   case 'PUT':
   case 'DELETE':
      parse_str(file_get_contents("php://input"),$var_array);
      ...
      break;
   default:
      echo "I don't know how to handle this request.";
}

Solution 3

In CodeIgniter 4 use getRawInput which will retrieve data and convert it to an array.

$data = $request->getRawInput();

Solution 4

look this issue in github

PUT parameters only work in JSON format

https://github.com/chriskacerguis/codeigniter-restserver/issues/362

Solution 5

Checkout this link in the official Code Igniter Docs Using the Input Stream for Custom Request Methods

This is the Code Igniter way to do it.

Just call the below if the body of the request is form-urlencoded

$var1 = $this->input->input_stream('var_key')
// Or 
$var1 = $this->security->xss_clean($this->input->input_stream('var_key'));
Share:
28,213

Related videos on Youtube

ncrocfer
Author by

ncrocfer

Python & PHP / Symfony2 dev'

Updated on July 09, 2022

Comments

  • ncrocfer
    ncrocfer over 1 year

    I have a problem right now with CodeIgniter : I use the REST Controller library (which is really awesome) to create an API but I can not get PUT requests...

    This is my code :

    function user_put() {
        $user_id = $this->get("id");
        echo $user_id;
        $username = $this->put("username");
        echo $username;
    }
    

    I use curl to make the request :

    curl -i -X PUT -d "username=test" http://[...]/user/id/1
    

    The user_id is full but the username variable is empty. Yet it works with the verbs POST and GET. Have you any idea please?

    Thank you !

  • jcolebrand
    jcolebrand almost 12 years
    This may have been accurate in April 2011, but it is not currently accurate.

Related