function format_script($source) { $html = strtolower($source); //如果是json则转换成xml,返回 $xml = json2xml($html); if ($xml !== false) { return $xml; } //如果是html则忽略头部,否则返回 $i = strpos($html, '<body'); if ($i === false) { return $html; } $html = mb_strcut($html, $i); $regex = array("#<script[^>]*?>.*?</script>#si", "#<style[^>]*?>.*?</style>#si", "#<!--[/!]*?[^<>]*?>#si"); $replace = array('', '', ''); $html = preg_replace($regex, $replace, $html); // $html = html_entity_decode($html,ENT_QUOTES); return $html; }
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; }