gpt4 book ai didi

php - 如何以编程方式直接从 PHP 中的数组 HTTP POST CSV 文件?

转载 作者:搜寻专家 更新时间:2023-10-31 21:07:59 25 4
gpt4 key购买 nike

我正在向 opencellid.org 网站贡献数据。他们有多个 API 选项来提交数据,但唯一的批量方法需要使用 API key 作为 POST 字段上传 HTTP POST 文件。可接受的格式为 CSV、JSON 和 CLF3。我将数据存储在 SQL 数据库中供内部使用,并定期将数据提交到 opencellid。

目前,我用来将数据提交到 opencellid 的脚本会查询 SQL 数据库以获取最近的测量值,然后将其作为 CSV 文件保存在服务器上,然后立即通过 HTTP POST 上传。在我看来,这是不雅的。

所以我的问题是,您能否直接从数组中通过 POST 方式上传 CSV 文件,而不必首先在服务器上实际创建 CSV 文件?

这是我们目前使用的代码片段。

//Create and save CSV
$output = fopen("opencellid.csv","w") or die();
fputcsv($output, array_keys($celldata[0]));

foreach($celldata as $cell) {
fputcsv($output, $cell);
}

fclose($output) or die();

//Upload CSV
$target_url = 'http://opencellid.org/measure/uploadCsv';

$file_name_with_full_path = realpath('./opencellid.csv');

$post = array('key' => 'opencellidapikey',
'datafile'=>'@'.$file_name_with_full_path.";type=text/plain");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

$postresult = curl_exec ($ch);

谁能建议一种直接从数组上传 CSV 的方法?

最佳答案

我设法将 csv 数据捕获到一个变量中,使用 fputcsv to php://output 输出缓冲。由于该服务仅允许提交多部分格式,因此您需要像这样构造有效负载。

<?php

//Construct your csv data
$celldata = array(
array(
"heading1",
"heading2",
"heading3",
),
array(
1,
2,
3,
),
array(
4,
5,
6,
)
);

//Output to php://output
ob_start();
$outstream = fopen("php://output", 'w');
foreach($celldata as $cell) {
fputcsv($outstream, $cell);
}
fclose($outstream) or die();
$csv_data = ob_get_clean();

$url = 'http://opencellid.org/measure/uploadCsv';
// Taken from http://stackoverflow.com/questions/3085990/post-a-file-string-using-curl-in-php

// form field separator
$delimiter = '-------------' . uniqid();
// file upload fields: name => array(type=>'mime/type',content=>'raw data')
$fileFields = array(
'datafile' => array(
'type' => 'text/plain',
'content' => $csv_data,
),
);
// all other fields (not file upload): name => value
$postFields = array(
'key' => 'opencellidapikey', //Put your api key here
);

$data = '';

// populate normal fields first (simpler)
foreach ($postFields as $name => $content) {
$data .= "--" . $delimiter . "\r\n";
$data .= 'Content-Disposition: form-data; name="' . $name . '"';
$data .= "\r\n\r\n";
$data .= $content;
$data .= "\r\n";
}
// populate file fields
foreach ($fileFields as $name => $file) {
$data .= "--" . $delimiter . "\r\n";
$data .= 'Content-Disposition: form-data; name="' . $name . '";' .
' filename="' . $name . '"' . "\r\n";
$data .= 'Content-Type: ' . $file['type'] . "\r\n";
$data .= "\r\n";
$data .= $file['content'] . "\r\n";
}
$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_setopt($handle, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($handle);
var_dump($result);

我收到 API key 错误,但它应该可以工作。

关于php - 如何以编程方式直接从 PHP 中的数组 HTTP POST CSV 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29026952/

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