在PHP中使用cURL POST文件字符串?

gaw*_*ron 35 php upload post curl file

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

我知道您可以通过在文件路径前面添加"@"来发布文件系统中已有的文件.

但是我想绕过创建一个临时文件并仅将该文件作为字符串发送,但我不确定如何使用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'
        )
    );
Run Code Online (Sandbox Code Playgroud)

Pis*_*3.0 27

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

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--
Run Code Online (Sandbox Code Playgroud)

因此,如果我们自己构建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);
Run Code Online (Sandbox Code Playgroud)

这样,我们自己做了所有繁重的工作,并相信cURL不会破坏它.

  • 当在第一个`foreach`结束时填充`$ postFields`时,你缺少`$ data.= $ content."\ r \n";`否则不会发送字段内容. (7认同)
  • 我已经构建了Piskvor建议的请求,是的,你可以自己构建身体,它会起作用.虽然您必须100%准确地使用EOL.在"内容长度"和"内容处理"结束时,需要双重回报.对于"Content-Type:text/plain"之后的文件,还需要双返回.对于身体值,分隔符也略微不同于"边界"中的分隔符.每个边界线在开头前都有两个额外的连字符.最后一个分隔符在开始和结束时有两个额外的超额. (2认同)
  • @Chris Henry:的确如此.我建议你阅读这个问题; 引用:"我知道你可以通过在文件路径前添加"@"来发布已经在文件系统上的文件.但是我想绕过创建一个临时文件......"所以,你的解决方案是"怎么做这没有@/some/file/name?" 是"使用@/some/file/name"?心灵,它令人难以置信...... (2认同)

小智 16

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)
);
Run Code Online (Sandbox Code Playgroud)

  • 它肯定会创建一个文件处理程序,我试了一下,但我认为CURLOPT_INFILE和CURLOPT_INFILESIZE用于PUT文件,$ _FILES数组中没有文件出现. (4认同)