Php 通过PUT向codeigniter REST\ U控制器发送文件
我正在尝试通过PUT将一些文件上传到web服务器 我正在服务器端使用Phil Sturgeon的REST库 客户端是一个使用curl生成请求的PHP应用程序Php 通过PUT向codeigniter REST\ U控制器发送文件,php,codeigniter,rest,curl,put,Php,Codeigniter,Rest,Curl,Put,我正在尝试通过PUT将一些文件上传到web服务器 我正在服务器端使用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); c
...
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_infle时如何使用curl?我希望能够发送大文件而不受php内存限制。真是一团糟。。。你可以在这里看到罪犯:
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
中。但是,如果未命中该,则执行parse_str
,这会愚蠢地添加下划线,并将结果作为$This->put()
数组中的键,不带任何值(因此数组\u flip
也不起作用)。哇
我似乎想不出一种方法让库找到$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中乱搞,但我不确定我的请求是否在一开始就正确格式化了。我终于又开始考虑这个问题。我在libraries/Format.php中添加了以下代码,以消除二进制($string){return$string;}中的异常(private function)
现在可以通过$this->request->body或$this->put()获得内容,如上面的答案所示。