Last active
August 29, 2015 13:57
-
-
Save avantassel/9709816 to your computer and use it in GitHub Desktop.
Download CSV and display in JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* @author Andrew Van Tassel | |
* @email [email protected] | |
* @notes Create a data directory, Add tac param to start from end of file | |
* @todo Add paging and offset param | |
*/ | |
$_GET['limit']=isset($_GET['limit']) && is_numeric($_GET['limit'])?(int)$_GET['limit']:50; | |
function getFieldKeys($output){ | |
$file = popen("head -1 $output", "r"); | |
$keys = array(); | |
if ($file) { | |
$buffer = fgets($file, 4096); | |
$line = explode(",", str_replace('"', '',$buffer)); | |
//remove crlf on last key | |
$line[count($line)-1]=str_replace("\r\n", "", $line[count($line)-1]); | |
$keys=$line; | |
} | |
pclose($file); | |
return $keys; | |
} | |
if(empty($_GET['file'])) | |
die('Need a file parameter with the URL of the CSV.'); | |
$tac = isset($_GET['tac'])?true:false; | |
$csv_url_arr = explode("/", $_GET['file']); | |
$file_name = end($csv_url_arr); | |
$output = __DIR__.'/data/'.$file_name; | |
$csv_url = $_GET['file']; | |
$last_updated = date('Ymd'); | |
$response = array('meta'=>array('file'=>$_GET['file'],'limit'=>$_GET['limit'],'tac'=>$tac),'data'=>array()); | |
if(file_exists($output)){ | |
//set the date of last updated | |
$last_updated=date('Ymd',filemtime($output)); | |
$response['meta']['last_updated']=$last_updated; | |
if($last_updated != date('Ymd')) | |
exec('mv '.$output.' '.str_replace('.csv', '-'.date('Ymd').'.csv', $output)); | |
} | |
if($last_updated != date('Ymd') || !file_exists($output)){ | |
//exec('curl '.$csv_url.' > '.$output); | |
set_time_limit(0); | |
$fp = fopen ($output, 'w+'); | |
$ch = curl_init($csv_url); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 50); | |
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
curl_exec($ch); // get curl response | |
curl_close($ch); | |
fclose($fp); | |
} | |
if(!file_exists($output)){ | |
die("CSV file not found, $output"); | |
} | |
//$response['meta']['file']=$output; | |
$response['meta']['size']=round(filesize($output)/(1024 * 1024),2)."MB"; | |
$totalLines = intval(exec("wc -l '$output'")); | |
$response['meta']['total']=!empty($totalLines)?($totalLines-1):0; | |
$keys = getFieldKeys($output); | |
$results=array(); | |
if($tac) | |
$handle = popen("tac $output", "r") or die("Couldn't get handle for $output"); | |
else | |
$handle = fopen("$output", "r") or die("Couldn't get handle for $output"); | |
if ($handle) { | |
while (!feof($handle)) { | |
$buffer = fgets($handle, 4096); | |
$line = explode(",", str_replace('"', '',$buffer)); | |
//remove crlf on last key | |
$line[count($line)-1]=str_replace("\r\n", "", $line[count($line)-1]); | |
//must have same number of elements | |
if(count($keys) != count($line) || !$tac && $keys===$line) | |
continue; | |
$result=array_combine($keys,$line); | |
$results[]=$result; | |
if(count($results) == 50) | |
break; | |
} | |
if($tac) | |
pclose($handle); | |
else | |
fclose($handle); | |
} | |
$response['data']=$results; | |
header("Content-type: application/json"); | |
echo json_encode($response); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment