在 PHP 中使用 cURL 发布文件字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3085990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
POST a file string using cURL in PHP?
提问by gawpertron
I was wondering if it is possible to post a file - along with other form data - when the file is just a string?
我想知道是否可以发布文件 - 以及其他表单数据 - 当文件只是一个字符串时?
I know that you can post a file that is already on the filesystem by prefixing the filepath with "@".
我知道您可以通过在文件路径前加上“@”来发布文件系统上已有的文件。
However I'd like to bypass creating a temporary file and send just the file as a string, but I am unsure how to construct the request using cURL in PHP.
但是,我想绕过创建临时文件并仅将文件作为字符串发送,但我不确定如何在 PHP 中使用 cURL 构造请求。
Cheers
干杯
$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'
)
);
采纳答案by Piskvor left the building
Should be possible: here's a form, posted through a browser (irrelevant fields omitted):
应该是可能的:这是一个通过浏览器发布的表单(省略了不相关的字段):
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--
So, if we build the POST body ourselves and set an extra header or two, we should be able to simulate this:
因此,如果我们自己构建 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);
This way, we're doing all the heavy lifting ourselves, and trusting cURL not to mangle it.
这样,我们自己做所有繁重的工作,并且相信 cURL 不会破坏它。
回答by erik.wiffin
php has access to a temporary location "php://memory", which actually makes what you're trying to do fairly easy.
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)
);

