【问题标题】:Sending a file via PUT to codeigniter REST_Controller通过 PUT 将文件发送到 codeigniter REST_Controller
【发布时间】:2014-02-28 08:56:02
【问题描述】:

我正在尝试通过 PUT 将一些文件上传到网络服务器。

我在服务器端使用 Phil Sturgeon 的 REST 库。

客户端是一个使用 curl 生成请求的 PHP 应用程序。

...
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch,CURLOPT_INFILE,$fp);
curl_setopt($ch,CURLOPT_INFILESIZE,$fsize);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$headarray = array();
if ($api_key)
    $headarray[] = 'X-API-KEY:'.$api_key;
$headarray[] = "Content-Type: application/octet-stream";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headarray);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_exec($ch);
...

正在接收数据。 但是,当我查看服务器端的 $this->put() 时,我得到一个看起来像我的输入文件已解析的数组。我希望将整个文件作为一个字符串作为原始数据提供。

我尝试改用fopen("php://input", "r");,但它是空白的。大概这已经被 REST 库使用了。

我在使用 curl 之前没有写过 PUT 请求,所以这方面可能有问题。

有没有 $this->put() 的替代方法,它会给我原始输入而不是数组。

看来我可能必须将我的文件放入一个参数中,如果是这样,当我使用 CURLOPT_INFILE 时如何使用 curl 执行此操作?我希望能够发送大文件而不会遇到 php 的内存限制。

【问题讨论】:

    标签: php codeigniter rest curl put


    【解决方案1】:

    真是一团糟……你可以在这里看到罪犯:

    REST_Controller.php - 行 ~950

    protected function _parse_put()
    {
        // It might be a HTTP body
        if ($this->request->format)
        {
            $this->request->body = file_get_contents('php://input');
        }
    
        // If no file type is provided, this is probably just arguments
        else
        {
            parse_str(file_get_contents('php://input'), $this->_put_args);
        }
    
    }
    

    if 会做你想做的事:将原始内容转储到$this->request->body。不过,这个if 没有被击中,所以它会执行parse_str,它愚蠢地添加下划线并将结果作为键放在$this->put() 数组中没有值(所以@987654327 @ 也不起作用)。哇。

    我似乎无法找到让图书馆找到$this->request->format true 的方法;如果您添加您正在使用的内容类型或更改 cURL 标头中的内容类型,我们会得到一个堆栈跟踪

    Fatal error:  Uncaught exception 'Exception' with message 'Format class does not support conversion from "stream".' in /Users/mycpu/Sites/ci-rest/application/libraries/Format.php:51
    Stack trace:
    #0 /Users/mycpu/Sites/ci-rest/application/libraries/Format.php(31): Format->__construct('Lorem ipsum Ut ...', 'stream')
    #1 /Users/mycpu/Sites/ci-rest/application/libraries/REST_Controller.php(251): Format->factory('Lorem ipsum Ut ...', 'stream')
    #2 /Users/mycpu/Sites/ci-rest/system/core/CodeIgniter.php(308): REST_Controller->__construct()
    #3 /Users/mycpu/Sites/ci-rest/index.php(202): require_once('/Users/mycpu...')
    #4 {main}
      thrown in /Users/mycpu/Sites/ci-rest/application/libraries/Format.php on line 51
    

    我认为解决此问题的最简单方法是将 parse_str 行更改为类似

    array_push($this->_put_args, file_get_contents('php://input'));
    

    那么纯正的php://input 将通过

    获得
    $p = $this->put();
    $p[0];//contents of file
    

    希望这至少能帮助你朝着正确的方向前进。

    【讨论】:

    • 感谢您为此付出的努力。我尝试了一些相同的方法,弄乱了 request->body,但我不确定我是否一开始就正确格式化了我的请求。
    • 我终于有时间再看看这个。我将以下代码添加到 library/Format.php 以消除异常 private function _from_binary($string){ return $string; } 现在可以通过 $this->request->body 或 $this->put() 获得内容,如您上面的答案。
    猜你喜欢
    • 2010-12-14
    • 2017-05-20
    • 1970-01-01
    • 2017-07-10
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多