【问题标题】:Cannot export response in csv format in Restler 3 rc5无法在 Restler 3 rc5 中以 csv 格式导出响应
【发布时间】:2014-08-31 07:08:06
【问题描述】:

我在 restler3 rc5 中以 csv 格式导出响应时遇到问题。

public function downloadCSV()
{
$array = array(
    "foo", "bar"
);

return $array;
}

我有支持的格式行

 $r->setSupportedFormats('CsvFormat', 'JsonFormat'); 

但我得到一个空的 csv。

请帮助我。如果有任何标准数组格式,我们需要返回以将其转换为 csv。请把格式发给我。

我已在以下页面查看

供应商/Luracast/Format/CsvFormat.php

public function encode($data, $humanReadable = false)
{
    $char = Object::$separatorChar;
    Object::$separatorChar = false;
    $data = Object::toArray($data);
    Object::$separatorChar = $char;
    if (is_array($data) && array_values($data) == $data) {
        //if indexed array
        $lines = array();
        $row = array_shift($data);
        if (array_values($row) != $row) {
            $lines[] = static::putRow(array_keys($row));
        }
        $lines[] = static::putRow(array_values($row));
        foreach ($data as $row) {
            $lines[] = static::putRow(array_values($row));
        }
        return implode(PHP_EOL, $lines) . PHP_EOL;
    }
    throw new RestException(
        500,
        'Unsupported data for ' . strtoupper(static::EXTENSION) . ' format'
    );
}

问题是在上述场景中返回的 $lines 是空的(即

$array = array( "foo", "bar" );

encode 函数中的 (array_keys($row) 和 array_values($row) 行是空的,因为 $row 不是数组,它是唯一的文本。

我被困了好几个小时。

非常感谢任何帮助。

【问题讨论】:

    标签: restler


    【解决方案1】:

    我终于找到了数组的格式,使其在 csv 导出中可用

     $array [] = array("foo" =>"foo","bar"=>"bar","vvvv"=>"123");
    

    我希望这对其他人有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      • 2019-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多