【问题标题】:Upload File via RESTful API?通过 RESTful API 上传文件?
【发布时间】:2011-10-18 18:09:56
【问题描述】:

我试图通过 POST 方法调用 RESTful API 来上传视频。我缺乏的是我不知道编写这种 API 的最佳实践,我也没有在互联网上找到任何可以遵循的资源。现在我正在这样做:

我正在使用 PHP 和 zend 框架 ( Zend_Rest_Route )。

第一种方法:

在客户端使用 file_get_contents 并使用 curl 将其 POST 到 API,在服务器端使用 file_put_contents 写入该数据并发送适当的响应。

第二:

使用 Zend_File_Treasfer 在服务器端接收文件,并将我的上传 api 端点地址放在 zend_form 中,设置方法为 post。在这种情况下,文件上传到服务器,但提交表单后,地址栏中的 url 指向 api 服务器,并且永远不会回到表单。

我做得对吗?如果不对,请告诉我最佳做法是什么以及如何做到这一点。

感谢您的宝贵时间。

【问题讨论】:

    标签: php zend-framework rest architecture file-upload


    【解决方案1】:

    您是否尝试过将重定向添加到处理上传的控制器操作的末尾? (如果不是,你真的应该在发布后重定向作为它的好习惯)(确保在你的逻辑执行后重定向)。本质上,接收发布数据的“页面”应该只处理数据,并且您想要返回给用户的有关发布操作的任何信息都应该在您重定向到的页面上提供给他们。

    [form]--POST-->['post'控制器动作]--redirect (302)-->[page with success/failure info]

    【讨论】:

      【解决方案2】:

      这样的事情对我有用:

      public function postAttachment($fileName, $fileMimetype, $fileContents, $postURL, $username, $password) 
      {
          $auth = base64_encode($username . ':' . base64_decode($password));
          $header = array("Authorization: Basic ".$auth);
          array_push($header, "Accept: */*");     
          $boundary =  "----------------------------".substr(md5(rand(0,32000)), 0, 12); 
      
          $data = ""; 
          $data .= "--".$boundary."\r\n"; 
      
          //Collect Filedata          
          $data .= "Content-Disposition: form-data; name=\"file\"; filename=\"".$fileName."\"\r\n"; 
          $data .= "Content-Type: ".$fileMimetype."\r\n"; 
          $data .= "\r\n";
          $data .= $fileContents."\r\n"; 
          $data .= "--".$boundary."--";
          // add more parameters or files here
      
          array_push($header, 'Content-Type: multipart/form-data; boundary='.$boundary);
          $params = array('http' => array( 
             'method' => 'POST', 
             'protocol_version' => 1.1, 
             'user_agent' => 'File Upload Agent',
             'header' => $header, 
             'content' => $data 
          )); 
         $ctx = stream_context_create($params); 
         $fp = fopen($postURL, 'rb', false, $ctx); 
      
         if (!$fp) { 
            throw new Exception("Problem with ".$postURL." ".$php_errormsg); 
         } 
         $responseBody = @stream_get_contents($fp); 
         if ($responseBody === false) { 
            throw new Exception("Problem reading data from ".$postURL.", ".$php_errormsg); 
         } 
      }
      

      如果您想发布多个文件,或添加其他多部分参数,也可以轻松地将这些添加到其他边界。

      我在另一篇文章中找到了其中一些代码,您可能可以在 PHP wiki (http://www.php.net/manual/en/function.stream-context-create.php#90411) 中找到类似的代码)。但是......该代码没有正确处理回车+换行符,我的服务器立即拒绝了该帖子。此外,旧代码还使用 HTTP 1.0 版——(它不重用套接字)。使用 HTTP 1.1 时,在发布大量文件时会重新使用套接字。 (这也适用于 HTTPS。)我添加了自己的用户代理 - 如果您正在欺骗某些服务器认为这是浏览器帖子,您可能需要更改用户代理以欺骗浏览器。

      【讨论】:

        猜你喜欢
        • 2021-07-22
        • 2012-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-26
        • 2013-09-01
        • 2023-03-10
        • 1970-01-01
        相关资源
        最近更新 更多