简体   繁体   English

Luracast Restler是否支持多部分/表单数据格式?

[英]Does Luracast Restler support multipart/form-data format?

I have a sample Restler class: 我有一个示例Restler类:

   class Author {
      ....

      function post($request_data=NULL) {
         var_dump($request_data);
         var_dump($_FILES);
         var_dump($_REQUEST);

         return $this->dp->insert($this->_validate($request_data));
       }

      ....
  }

I'm trying to POST file and some data to Restler service by simple HTML form: 我正在尝试通过简单的HTML形式将文件和一些数据发布到Restler服务中:

  <FORM action="http://host/index.php/author" enctype="application/x-www-form-urlencoded" method="post">
      Name:  <INPUT type="text" name="name" value="dima"><BR>
      Email: <INPUT type="text" name="email" value="dima@prot.lt"><BR>
      File:  <INPUT type="file" name="files"><BR> 
      <INPUT type="submit" value="Send"> <INPUT type="reset">
  </FORM>

It is clear, that the $_FILES array will be empty, but $_REQUEST and $request_data will have three variables: name = "dima" , email = "dima@prot.lt" and file = "selected file name" . 显然, $_FILES数组将为空,但是$_REQUEST$request_data将具有三个变量: name = "dima"email = "dima@prot.lt"file = "selected file name"

In the next test, I changed the form enctype value to multipart/form-data. 在下一个测试中,我将表单enctype值更改为multipart / form-data。

  <FORM action="http://host/index.php/author" enctype="multipart/form-data" method="post">
      Name:  <INPUT type="text" name="name" value="dima"><BR>
      Email: <INPUT type="text" name="email" value="dima@proto.lt"><BR>
      File:  <INPUT type="file" name="files"><BR> 
      <INPUT type="submit" value="Send"> <INPUT type="reset">
  </FORM>

When I press submit form, in the $_REQUEST array, I will see the same three variables, the $_FILES array will be filled with uploaded file information, BUT the $request_data array will be empty !! 当我按提交表单时,在$_REQUEST数组中,我将看到相同的三个变量, $_FILES数组将填充上载的文件信息, $request_data数组将为空

Can anybody help in this situation? 在这种情况下有人可以帮忙吗? Where have I made a mistake? 我在哪里弄错了?

I have solved this issue, by writing Restler plugin, may be other will have the same problem: 我已经通过编写Restler插件解决了这个问题,可能其他都会有同样的问题:

<?php

/**
 * URL Encoded String in multipart data format
 * @category   Framework
 * @author     Dmitrij Orlov <dmitrij.orlov@gmail.com>
*/
class UrlMultipartFormat implements iFormat
{
    const REQUEST_MIME      = 'multipart/form-data';
    const RESPONCE_MIME     = 'application/json';
    const EXTENSION         = 'post';

    public function getMIMEMap() {
        return array(self::EXTENSION=>self::REQUEST_MIME);
    }

    public function getMIME(){
        return self::RESPONCE_MIME;
    }

    public function getExtension(){
        return self::EXTENSION;
    }

    public function setMIME($mime){
        //do nothing
    }

    public function setExtension($extension){
        //do nothing
    }

    public function encode($data, $human_readable=FALSE){
        return $human_readable ? 
        $this->json_format(json_encode(object_to_array($data))) : 
        json_encode(object_to_array($data));
    }

    public function decode($data){
        return $_REQUEST;
    }

    public function __toString(){
        return $this->getExtension();
    }

    /**
     * Pretty print JSON string
     * @param string $json
     * @return string formated json
     */
    private function json_format($json)
    {
        $tab = "  ";
        $new_json = "";
        $indent_level = 0;
        $in_string = FALSE;

        $len = strlen($json);

        for($c = 0; $c < $len; $c++) {
            $char = $json[$c];
            switch($char) {
                case '{':
                case '[':
                    if(!$in_string) {
                        $new_json .= $char . "\n" . 
                        str_repeat($tab, $indent_level+1);
                        $indent_level++;
                    } else {
                        $new_json .= $char;
                    }
                    break;
                case '}':
                case ']':
                    if(!$in_string) {
                        $indent_level--;
                        $new_json .= "\n".str_repeat($tab, $indent_level).$char;
                    } else {
                        $new_json .= $char;
                    }
                    break;
                case ',':
                    if(!$in_string) {
                        $new_json .= ",\n" . str_repeat($tab, $indent_level);
                    } else {
                        $new_json .= $char;
                    }
                    break;
                case ':':
                    if(!$in_string) {
                        $new_json .= ": ";
                    } else {
                        $new_json .= $char;
                    }
                    break;
                case '"':
                    if($c==0){
                        $in_string = TRUE;
                    }elseif($c > 0 && $json[$c-1] != '\\') {
                        $in_string = !$in_string;
                    }
                default:
                    $new_json .= $char;
                    break;
            }
        }

        return $new_json;
    }

}

?>

Restler v2.1 and below does not support multipart/form-data as we are focusing on API creation. Restler v2.1和更低版本不支持multipart/form-data因为我们专注于API创建。 But we are open to ideas and will consider adding it to the future versions of it. 但是我们愿意接受想法,并会考虑将其添加到将来的版本中。

There is nothing wrong in using $_REQUEST and $_FILES directly in your function and currently that is the only option to get the data 直接在函数中使用$_REQUEST$_FILES没什么错,当前这是获取数据的唯一选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM