2011-12-15 38 views
0

我有一个样品Restler类:Luracast Restler是否支持multipart/form-data格式?

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

     .... 
    } 

我想POST文件,并通过简单的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="[email protected]"><BR> 
     File: <INPUT type="file" name="files"><BR> 
     <INPUT type="submit" value="Send"> <INPUT type="reset"> 
    </FORM> 

这是明确的,即$_FILES数组为空,但$_REQUEST$request_data将具有三个变量: name = "dima"email = "[email protected]"file = "selected file name"

在接下来的测试中,我将表单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="[email protected]"><BR> 
     File: <INPUT type="file" name="files"><BR> 
     <INPUT type="submit" value="Send"> <INPUT type="reset"> 
    </FORM> 

当我按下提交表单,该$_REQUEST阵中,我将看到相同的三个变量中,$_FILES阵列将充满上传的文件信息,$request_data阵列将为空

有人可以帮助在这种情况下?我在哪里犯了一个错误?

回答

1

我已经解决了这个问题,通过编写Restler插件的数据是唯一的选择,可能是其他具有相同的问题:

<?php 

/** 
* URL Encoded String in multipart data format 
* @category Framework 
* @author  Dmitrij Orlov <[email protected]> 
*/ 
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; 
    } 

} 

?> 
+0

在Restler问题页面阅读[this thread](https://github.com/Luracast/Restler/issues/26)以获取更多详细信息 – Luracast 2012-02-22 04:10:45

0

因为我们专注于API创建,所以Restler v2.1及更低版本不支持multipart/form-data。但我们愿意接受创意,并会考虑将其添加到未来的版本中。

没有什么错在你的函数使用$_REQUEST$_FILES直接,目前是让