public function csvAction() { $this->load(); $this->render("none", null, true); header("Content-type: text/csv; charset=UTF-8"); header("Content-Disposition: attachment; filename=\"" . pagename() . ".csv\""); //output bom for utf-8 (so that excel will open this as UTF-8 file! UTF-8 should not need BOM but Excel doesn't know that.. echo chr(239) . chr(187) . chr(191); $xmlpath = $this->view->getScriptPath("") . pagename() . "/xml.phtml"; if (file_exists($xmlpath)) { $xml_content = $this->view->render(pagename() . "/xml.phtml"); require_once "app/xml2csv.php"; xml2csv($xml_content); } else { echo "No CSV content is available for this page."; } }
function curl_request($url, $accept) { //Ricevo l'arrey con gli header della risposta $arrey_headers = get_headers($url, 1); //Salvo il Content-Type della risposta $content = $arrey_headers["Content-Type"]; //Richiesta CURL if (!function_exists('curl_init')) { die('Sorry cURL is not installed!'); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); // Include header in result? (0 = yes, 1 = no) curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: text/turtle, text/csv, application/json, application/xml")); // Should cURL return or print out the data? (true = return, false = print) curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($ch); curl_close($ch); //Genero nome random per il file temporaneo $nomefiletemp = random_string(5); //Creo un file temporaneo dove salvare il risultato $ourFileHandle = fopen($nomefiletemp . ".txt", 'w+') or die("can't open file"); fwrite($ourFileHandle, $output); fclose($ourFileHandle); //Leggo il Content-type della risposta e se necessario converto switch ($content) { case "application/json": $xml = json2xml($nomefiletemp . ".txt"); break; case "text/csv": $xml = csv2xml($nomefiletemp . ".txt"); break; case "text/turtle": $xml = turtle2xml($nomefiletemp . ".txt"); break; case "application/xml": $xml = $output; break; //Errore //Errore case "text/plain": sendHTTPError($output, 'Sorgente errore --> aggregatore'); break; default: unlink($nomefiletemp . ".txt"); sendHTTPError('Attenzione', 'Formato ricevuto non accettato, Sorgente errore --> aggregatore'); break; } //remove temp file unlink($nomefiletemp . ".txt"); //Genero nome random per il file temporaneo $nomefiletemp2 = random_string(5); //Creo file temporaneo con la risposta convertita in formato XML $ourFileHandle2 = fopen($nomefiletemp2 . ".xml", 'w+') or die("can't open file"); fwrite($ourFileHandle2, $xml); fclose($ourFileHandle2); //Converto nel formato richiesto dal narratore switch ($accept) { case "application/json": $result = xml2json($nomefiletemp2 . ".xml"); break; case "text/csv": $result = xml2csv($nomefiletemp2 . ".xml"); break; case "text/turtle": $result = turtle2xml($nomefiletemp2 . ".xml"); break; case "application/xml": $result = $xml; break; default: unlink($nomefiletemp2 . ".xml"); sendHTTPError('Attenzione', 'Formato richiesto non accettato'); break; } //remove temp file unlink($nomefiletemp2 . ".xml"); return $result; }