php 使用 Codeigniter 获取 PUT 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5540781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:46:03  来源:igfitidea点击:

get a PUT request with Codeigniter

phpcodeigniterrest

提问by ncrocfer

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...

我现在有一个 CodeIgniter 问题:我使用REST 控制器库(这真的很棒)来创建一个 API,但我无法获得 PUT 请求......

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 提出请求:

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?

user_id 已满,但 username 变量为空。然而,它适用于动词 POST 和 GET。你有什么想法吗?

Thank you !

谢谢 !

回答by jcolebrand

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#L544to see that this method:

根据: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';
}

looks to see if we've defined the HTTP header HTTP_X_HTTP_METHOD_OVERRIDEand 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-Methodinstead of X-HTTP-Method-Overrideso this varies by framework.

查看我们是否定义了 HTTP 标头HTTP_X_HTTP_METHOD_OVERRIDE,并使用它来支持我们在网络上实现的实际动词。要在请求中使用它,您需要指定标头X-HTTP-Method-Override: method(so X-HTTP-Method-Override: put) 以生成自定义方法覆盖。有时框架期望X-HTTP-Method而不是X-HTTP-Method-Override因此这因框架而异。

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

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

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

回答by Johnny2k

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.

您可以尝试先检测方法类型并区分不同的情况。如果您的控制器只处理 REST 函数,那么在构造函数中放入 get 所需的信息可能会有所帮助。

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

回答by do01

look this issue in github

在github上看这个问题

PUT parameters only work in JSON format

PUT 参数仅适用于 JSON 格式

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

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

回答by francis94c

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

在官方 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

如果请求的主体是表单 urlencoded,只需调用下面的

$var1 = $this->input->input_stream('var_key')
// Or 
$var1 = $this->security->xss_clean($this->input->input_stream('var_key'));

回答by fire

CodeIgniter doesn't support reading incoming PUT requests and if it's not essential I would stick to GET/POST for your API as its probably not necessary.

CodeIgniter 不支持读取传入的 PUT 请求,如果它不是必需的,我会坚持为您的 API 使用 GET/POST,因为它可能没有必要。

If you do need to read PUT requests take a look at Accessing Incoming PUT Data from PHP.

如果您确实需要阅读 PUT 请求,请查看从 PHP 访问传入的 PUT 数据