加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 使用Codeigniter获取PUT请求

发布时间:2020-12-13 13:31:26 所属栏目:PHP教程 来源:网络整理
导读:我现在遇到CodeIgniter的问题:我使用 REST Controller library(这真的很棒)来创建API但是我无法获得PUT请求… 这是我的代码: function user_put() { $user_id = $this-get("id"); echo $user_id; $username = $this-put("username"); echo $username;} 我
我现在遇到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");
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读