I am trying to use a web API to get some data. The problem is that I need to convert CSV data to JSON format.
<?php
// allows us to skip the first row while looping through the file
$i = 0;
//web api
$stocks = "https://www.google.com/finance/historical?output=csv&q=aapl";
//read csv file
$handle = fopen($stocks, 'r');
//loop through CSV file
while (($data = fgetcsv($handle, 2000, ",")) !== FALSE) {
if ($i > 0) {
trim($data[0]);
trim($data[1]);
trim($data[2]);
trim($data[3]);
trim($data[4]);
trim($data[5]);
// an array
$chartArray[] = $data;
}
$i++;
}
fclose($handle);
//Convert PHP Array to JSON String
print (json_encode($chartArray));
?>
As you can see from the image, I am getting JSON with enclosure. ["19-May-17","153.38","153.98","152.63","153.06","26960788"]
.
Please let me know if there is a solution. Thank you in advance!