Skip to content

Instantly share code, notes, and snippets.

@woduda
Last active August 29, 2015 13:56
Show Gist options
  • Save woduda/9113895 to your computer and use it in GitHub Desktop.
Save woduda/9113895 to your computer and use it in GitHub Desktop.
Simple JSON to CSV conversion
<?php
// echo $_SERVER['QUERY_STRING'];
$json = file_get_contents('http://api.fap.pl/dealers?'.$_SERVER['QUERY_STRING']);
$_json = json_decode($json, TRUE);
$out = '';
foreach($_json as $row)
{
$out .= substr(row($row)."\n", 1);
}
header("Content-Description: File Transfer");
header("Content-Type: text/csv; charset=windows-1250");
header('Content-Disposition: attachment; filename="out.csv"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: '.mb_strlen($out));
echo iconv('UTF-8', 'Windows-1250//TRANSLIT', $out);
function row($row)
{
$ret = '';
foreach ($row as $v)
{
if (is_array($v))
{
$ret .= row($v);
}
else
{
$ret .= ';"'.str_replace('"', '""', $v).'"';
}
}
return $ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment