gpt4 book ai didi

php - 在 PHP 中使用 cURL 发布文件字符串?

转载 作者:IT王子 更新时间:2023-10-29 01:02:39 24 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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 正文并设置一两个额外的 header ,我们应该能够模拟:

// 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 不会破坏它。

关于php - 在 PHP 中使用 cURL 发布文件字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3085990/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com