StreamedResponse symfony3上的ERR_INVALID_RESPONSE

时间:2017-02-28 12:18:02

标签: php csv symfony

我正在尝试根据通过API提取的信息生成CSV文件。在添加StreamedResponse之前,我只是看到CSV输出echo'd。一旦我添加了StreamedResponse,我收到一个ERR_INVALID_RESPONSE。

我正在使用Symfony3,但我相对较新。我不知道为什么StreamedResponse导致了err_invalid_response。

$callback = function() {
    $handle = fopen('php://output', 'w+');

    fputcsv($handle, $fieldNames,',');

    $data = $elq->get($query); //query is created before the callback function, as it's reused.

    foreach ($data->elements as $record) {
        $recordData = [];
        /* Parsing data from API into $recordData */
        fputcsv($handle,$recordData,",");
    }

    fclose($handle);
}
return new StreamedResponse($callback, 200, array(
    'Content-Type' => 'text/csv; charset=utf-8',
    'Content-Disposition' => 'attachment; filename="'.date("Ymd_His").'_'.ucfirst($settings->getType()).'_export.csv"'
));

1 个答案:

答案 0 :(得分:2)

这似乎是一个简单的变量范围问题。在回调函数中,有未知变量($ fieldNames,$ elq,$ query)。

我认为你可以这样做:

include_once "class1.php";
     class class2 
     {
         private $classs;

         public function __construct()
         {
              $this->classs = new class1();
         } 

         public function test_class2()
         {
             return $this->classs->test_class();
         }
      }