Wizarduss
Wizarduss

Reputation: 33

ERR_INVALID_RESPONSE on StreamedResponse symfony3

I'm trying to generate a CSV file based on info pulled via an API. Before adding the StreamedResponse, I simply see the CSV output echo'd. Once I add the StreamedResponse, I receive an ERR_INVALID_RESPONSE.

I'm using Symfony3, but I'm relatively new to it. I have no clue why the StreamedResponse is causing the 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"'
));

Upvotes: 0

Views: 1552

Answers (1)

Alphonse D.
Alphonse D.

Reputation: 571

This seems to be a simple variable scope problem. Inside your callback function, there are unknown variables ($fieldNames, $elq, $query).

I think that you can do something like that :

$callback = function() use($fieldNames, $elq, $query) {
    // your code
};

Upvotes: 2

Related Questions