task by sigma

Call test api (https://reqres.in/api/users?per_page=20) and create a csv/xls file with that data. with heading id, email, first_name, last_name, avatar (This heading should match with API parameter name and not hard coded)

2. Reading a test csv file(comma separated values) with only file name starting with company (company12344567788.csv) from specific location(d:/files) with dummy Data, store it into database, retrieve data from database remove duplicates (9 to 22 characters) and create a csv report with unique data. Eg: three csv files in d:/files  company12345.csv, test.csv,test1.csv. Should pick only company12345.csv. Read and store it into database. Retrieve and remove duplicates. Create a csv with name output.csv and write data to that.


1

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Call test api (https://reqres.in/api/users?per_page=20)
// and create a csv/xls file with that data. with heading id, email, first_name, last_name, avatar
// (This heading should match with API parameter name and not hard coded) php
// Function to fetch data from API
function fetchDataFromAPI($url) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
// return $response;
return json_decode($response, true);
}

// API URL
$apiUrl = 'https://reqres.in/api/users?per_page=20';

// Fetch data from API
$data = fetchDataFromAPI($apiUrl);
if ($data && isset($data['data'])) {
$filePath = 'users_data.csv';
$file = fopen($filePath, 'w');
if ($file === false) {
die('Failed to open CSV file for writing.');
}
fputcsv($file, array_keys($data['data'][0]));
foreach ($data['data'] as $user) {
fputcsv($file, $user);
}
fclose($file);
echo "CSV file '$filePath' created successfully.";
} else {
echo "Failed to fetch data from the API.";
}

?>

2


=

No comments:

Post a Comment

server laravel application

 asset_url = domain/public chmod -R 755 public/admin/ composer dump-autoload get the application from hostinger