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

标签 php csv post curl fputcsv

我正在向 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/

相关文章:

php - 在 PHP 中将十六进制代码转换为可读字符串

php - git 子模块 svn 外部

php - Symfony2,FOSRestBundle - 捕获异常

python - 使用AWS lambda函数在S3中创建新文件

csv - Grails:将已解析的CSV数据保存到数据库

javascript - 使用带有ajax的post请求将html表单数据发送到php到https ://foo. com

php - 通知 : Undefined property: stdClass: error

Python CSV 删除空行

python - 如何调试文件上传?

PHP:如何将 $_POST 中的多个复选框数组保存在变量中