【问题标题】:How to return a csv using a curl?如何使用 curl 返回 csv?
【发布时间】:2019-11-25 20:54:27
【问题描述】:

为我查询数据的 PHP 应用程序构建 API,将其放入对象数组中,例如:

success: [
  {name: 'X', isActive: true},
  {name: 'Y', isActive: true},
  {name: 'Z', isActive: true}
]

我的 PHP 代码:

private function receiptHistoryToCsv($rowData) {
        $filename = "export.csv";
        $delimeter=";";
        header('Content-Type: application/csv');
        header('Content-Disposition: attachment; filename="'.$filename.'";');

        // Initialize a file pointer for the csv
        $file = fopen('php://output', 'w');

        foreach ($rowData as $row) {
            fputcsv($file, $row, $delimeter);
        }

        fclose($file);

        return $file;
}

public function getCSV() {

  $tableHeaders = array ('name', 'is active');
  $rowData = array();

  // Add headers (acting like column names)
  array_push($rowData, $tableHeaders);

  $information = getInfoUsingDBQuery();

  foreach($information as $row) {
    $name = strval($row["name"]);
    $isActive = $row["isActive"];
    array_push($rowData, array($name, $is_active));
  }

  // $rowData now looks like the success array above.

  return receiptHistoryToCsv($rowData);
}

Endpoint:localhost:8080/api/getCSV

在前端,有一个带有文本Download CSV 的按钮。

如何将数组转换为 csv 文件,然后在单击按钮时以可下载格式返回 csv?

对于初学者,我只想转到端点并获取 csv 以开始自动下载。

【问题讨论】:

  • edit 提出您的问题,并向我们展示您尝试过的未按预期工作的内容。你想做的当然是可行的,但恐怕我们不是来给你写代码的。
  • 我的错。我将添加我的代码。

标签: php api csv download


【解决方案1】:

我只是能够让它工作。

public function receiptHistoryToCsv($rowData) {
        $filename = "export.csv";

        header('Content-Type: application/csv');
        header('Content-Disposition: attachment; filename="'.$filename.'";');

        $file = fopen('php://output', 'w');

        foreach ($rowData as $row) {
            fputcsv($file, $row);
        }

        fclose($file);

        // This is probably what made it work.
        ob_flush();
}

public function getCSV() {

  $tableHeaders = array ('name', 'is active');
  $rowData = array();

  // Add headers (acting like column names)
  array_push($rowData, $tableHeaders);

  $information = getInfoUsingDBQuery();

  foreach($information as $row) {
    $name = strval($row["name"]);
    $isActive = $row["isActive"];
    array_push($rowData, array($name, $is_active));
  }

  // $rowData now looks like the success array above.

  return this->receiptHistoryToCsv($rowData);
}

【讨论】:

    【解决方案2】:

    您可以直接输出getInfoUsingDBQuery()函数的结果,而无需将它们读入中间数组。

    public function getCSV() {
        header('Content-Type: application/csv');
        header('Content-Disposition: attachment; filename="export.csv";');
        $keys = ['name' => 'name', 'isActive' => 'is active'];
        $delimiter=";";
        $file = fopen('php://output', 'w');
        fputcsv($file, $keys, $delimiter);
        foreach (getInfoUsingDBQuery() as $row) {
            fputcsv($file, array_intersect_key($row, $keys), $delimiter);
        }
    }
    

    您不需要显式关闭输出流或刷新缓冲区,因为这会在脚本终止时自动发生。

    【讨论】:

      猜你喜欢
      • 2016-04-28
      • 2016-10-14
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      • 2014-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多