get a PUT request with Codeigniter

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

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