【问题标题】:POST a file string using cURL in PHP?在 PHP 中使用 cURL 发布文件字符串?
【发布时间】:2011-03-06 09:15:57
【问题描述】:

我想知道当文件只是一个字符串时,是否可以发布一个文件——连同其他表单数据?

我知道你可以通过在文件路径前加上“@”前缀来发布已经在文件系统上的文件。

但是我想绕过创建一个临时文件,只将文件作为字符串发送,但我不确定如何在 PHP 中使用 cURL 构造请求。

干杯

    $postFields = array(
        'otherFields'   => 'Yes'
        ,'filename'     => 'my_file.csv'
        ,'data'         => 'comma seperated content'
    );

    $options = array(
        CURLOPT_RETURNTRANSFER  => true
        ,CURLOPT_SSL_VERIFYPEER => false
        ,CURLOPT_SSL_VERIFYHOST => 1
        ,CURLOPT_POSTFIELDS     => $postFields
        ,CURLOPT_HTTPHEADER     => array(
            'Content-type: multipart/form-data'
        )
    );

【问题讨论】:

  • @Vin 绝对不是重复的。这是一个很好的问题。除了手动制作所有 POST 数据之外,我不知道任何添加部件标题的方法,就像这里需要的那样。
  • @Artefacto:对,我没有正确理解这个问题
  • 原始数据是指 file_get_contents($path_to_file); ?

标签: php file post upload curl


【解决方案1】:

应该可以:这是一个表单,通过浏览器发布(省略不相关的字段):

POST http://host.example.com/somewhere HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------------7da16b2e4026c
Content-Length: 105732

-----------------------------7da16b2e4026c
Content-Disposition: form-data; name="NewFile"; filename="test.jpg"
Content-Type: image/jpeg

(...raw JPEG data here...)
-----------------------------7da16b2e4026c
Content-Disposition: form-data; name="otherformfield"

content of otherformfield is this text
-----------------------------7da16b2e4026c--

所以,如果我们自己构建 POST 正文并设置一个或两个额外的标头,我们应该能够模拟这个:

// form field separator
$delimiter = '-------------' . uniqid();
// file upload fields: name => array(type=>'mime/type',content=>'raw data')
$fileFields = array(
    'file1' => array(
        'type' => 'text/plain',
        'content' => '...your raw file content goes here...'
    ), /* ... */
);
// all other fields (not file upload): name => value
$postFields = array(
    'otherformfield'   => 'content of otherformfield is this text',
    /* ... */
);

$data = '';

// populate normal fields first (simpler)
foreach ($postFields as $name => $content) {
   $data .= "--" . $delimiter . "\r\n";
    $data .= 'Content-Disposition: form-data; name="' . $name . '"';
    // note: double endline
    $data .= "\r\n\r\n";
}
// populate file fields
foreach ($fileFields as $name => $file) {
    $data .= "--" . $delimiter . "\r\n";
    // "filename" attribute is not essential; server-side scripts may use it
    $data .= 'Content-Disposition: form-data; name="' . $name . '";' .
             ' filename="' . $name . '"' . "\r\n";
    // this is, again, informative only; good practice to include though
    $data .= 'Content-Type: ' . $file['type'] . "\r\n";
    // this endline must be here to indicate end of headers
    $data .= "\r\n";
    // the file itself (note: there's no encoding of any kind)
    $data .= $file['content'] . "\r\n";
}
// last delimiter
$data .= "--" . $delimiter . "--\r\n";

$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_HTTPHEADER , array(
    'Content-Type: multipart/form-data; boundary=' . $delimiter,
    'Content-Length: ' . strlen($data)));  
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
curl_exec($handle);

这样,我们自己就可以完成所有繁重的工作,并且相信 cURL 不会破坏它。

【讨论】:

  • 最初误读了这个问题,并因此而被正确地否决了。改写成可以工作的东西。
  • 我已经按照 Piskvor 的建议构建了请求,是的,您可以自己构建主体,它会起作用。尽管您必须 100% 准确地使用 EOL。在“Content-Length”和“Content-Disposition”的末尾需要一个双重返回。对于“Content-Type: text/plain”之后的文件,还需要双回车。正文值的分隔符也与“边界”中未清除的分隔符略有不同。每条边界线的开头都有两个额外的连字符。最后一个分隔符在开头和结尾有两个额外的连字符。
  • @Chris Henry:确实。我建议你阅读这个问题;引用:“我知道您可以通过在文件路径前加上“@”前缀来发布文件系统上已经存在的文件。但是我想绕过创建临时文件......”所以,你的解决方案是“怎么做这没有 @/some/file/name 吗?”是“使用@/some/file/name”吗?头脑,它令人难以置信......
  • 在第一个foreach 的末尾填充$postFields 时,您缺少$data .= $content . "\r\n";,否则不会发送字段内容。
  • 谢谢,@Piskvor!关于 Content-Disposition 行中“name=”的注释。它应该设置为在给定给 CURLOPT_POSTFIELDS 的数组中将获得“@”文件的元素的名称。如果您使用 $params->file = "@myfilename.pdf" 在 curl_setopts CurlFile 中设置文件,那么您将需要 name="file"。我怀疑这与 HTML 中标签的“名称”相同。将其设置为与文件名相同可能不是服务器端想要的。
【解决方案2】:

php 可以访问临时位置“php://memory”,这实际上使您尝试做的事情变得相当容易。

$fh = fopen('php://memory','rw');
fwrite( $fh, $content);
rewind($fh);

$options = array(
    CURLOPT_RETURNTRANSFER  => true
    ,CURLOPT_SSL_VERIFYPEER => false
    ,CURLOPT_SSL_VERIFYHOST => 1
    ,CURLOPT_HTTPHEADER     => array(
        'Content-type: multipart/form-data'
    )
    ,CURLOPT_INFILE         => $fh
    ,CURLOPT_INFILESIZE     => strlen($content)
);

【讨论】:

  • 这是否允许与文件一起发送的其余后域?
  • 它确实创建了一个文件处理程序,我试了一下,但我认为 CURLOPT_INFILE 和 CURLOPT_INFILESIZE 用于 PUT 文件,$_FILES 数组中没有显示文件。
  • 问题中的代码可以一次上传多个文件。 PUTINFILE也可以上传多个文件吗?
  • 为什么要把逗号放在行首?它是 PHP,而不是 C,你可以用逗号结束所有项目。 [123, 456,]
猜你喜欢
  • 2011-09-03
  • 1970-01-01
  • 2021-12-12
  • 2016-03-17
  • 2012-12-27
  • 2016-02-20
  • 2011-11-21
相关资源
最近更新 更多