How can PHP be used to generate structured data outputs, such as CSV files, from API responses like the one provided in the forum thread?
To generate structured data outputs like CSV files from API responses in PHP, you can parse the API response data and format it into CSV format using PHP functions. You can use the `fputcsv()` function to write data to a CSV file line by line.
<?php
// Sample API response data
$apiResponse = '[{"id":1,"name":"John Doe","email":"johndoe@example.com"},{"id":2,"name":"Jane Smith","email":"janesmith@example.com"}]';
// Decode the API response JSON data
$data = json_decode($apiResponse, true);
// Open a new CSV file
$csvFile = fopen('output.csv', 'w');
// Write headers to the CSV file
fputcsv($csvFile, array_keys($data[0]));
// Write data rows to the CSV file
foreach ($data as $row) {
fputcsv($csvFile, $row);
}
// Close the CSV file
fclose($csvFile);
?>