使用Codeigniter获取PUT请求 [英] get a PUT request with Codeigniter

查看:478
本文介绍了使用Codeigniter获取PUT请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在遇到CodeIgniter的问题:我使用 REST控制器库(这真的很棒)创建一个API,但我不能得到PUT请求...



这是我的代码:

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

我使用curl提出请求:

  curl -i -X PUT -dusername = testhttp:// [...] / 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 查看此方法:

  / ** 
*检测方法
*
*检测哪个方法(POST,PUT,GET ,DELETE)正在使用
*
* @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 = 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:method (因此 X-HTTP-Method-Override:put )生成自定义方法覆盖。有时框架期望 X-HTTP-Method 而不是 X-HTTP-Method-Override



如果您通过jQuery执行此类请求,那么您可以将此块集成到您的ajax请求中:

  beforeSend:function(XMLHttpRequest){
//指定执行删除操作的HTTP方法DELETE。
XMLHttpRequest.setRequestHeader(X-HTTP-Method-Override,DELETE);
}


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 !

解决方案

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

这篇关于使用Codeigniter获取PUT请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆