我现在遇到CodeIgniter的问题:我使用
REST Controller library(这真的很棒)来创建API但是我无法获得PUT请求…
这是我的代码:
function user_put() { $user_id = $this->get("id"); echo $user_id; $username = $this->put("username"); echo $username; }
我使用curl来发出请求:
curl -i -X PUT -d "username=test" http://[...]/user/id/1
user_id已满,但username变量为空.然而它适用于动词POST和GET.
你有什么想法吗?
谢谢 !
根据:
http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/我们应该咨询
https://github.com/philsturgeon/codeigniter-restserver/blob/master/application/libraries/REST_Controller.php#L544,看看这个方法:
/** * 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'; }
看看我们是否定义了HTTP标头HTTP_X_HTTP_METHOD_OVERRIDE,它使用它来支持我们在网络上实现的实际动词.要在请求中使用它,您将指定标头X-HTTP-Method-Override:方法(因此X-HTTP-Method-Override:put)以生成自定义方法覆盖.有时框架需要X-HTTP-Method而不是X-HTTP-Method-Override,因此这会因框架而异.
如果你是通过jQuery做这样的请求,你会把这个块集成到你的ajax请求中:
beforeSend: function (XMLHttpRequest) { //Specify the HTTP method DELETE to perform a delete operation. XMLHttpRequest.setRequestHeader("X-HTTP-Method-Override","DELETE"); }