A php script I wrote to produce a CSV file is giving an error – PHP Fatal error: fputcsv(): Argument #2 ($fields) must be of type array

  Kiến thức lập trình
<?php
use someapiconnectionPersons;
try {  
    require_once('autoload.php');
    $PersonsAPI = new Persons();
    $data = $PersonsAPI->get();

    if (!empty($data)) {
        header('Content-Type: text/csv');
        header('Content-Disposition: attachment;filename=offline_persons.csv');

        $fp = fopen('php://output', 'wb');

        fputcsv($fp, array_keys($data));

        
        foreach ($data as $line) {
            $val = explode(",",$line);
            fputcsv($fp, $line);
        }

       
        fclose($fp);
    } else {
        echo "No data found";
    }
} catch (Exception $e) {
    echo "Error fetching data: " . $e->getMessage();
}

OUTPUT Error is: PHP Fatal error: Uncaught TypeError: fputcsv(): Argument #2 ($fields) must be of type array
Stack Trace: fputcsv(Resource id #15, Object(APIEthosPersons), ‘,’)

Followed by a list of numbers

0,1,2,3,4,5,….

New to php any help on where I am going wrong would be appreciated

New contributor

Joshua Walters is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT