Created
August 4, 2015 14:13
-
-
Save cbertelegni/41d12b1244b4008712c6 to your computer and use it in GitHub Desktop.
Get Url
This file contains hidden or 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 | |
$url = @(htmlspecialchars($_GET["url"])); | |
$key = @(htmlspecialchars($_GET["key"])); | |
$key2 = @(htmlspecialchars($_GET["key2"])); | |
$max_age = @mysql_real_escape_string(htmlspecialchars($_GET["max-age"])); | |
$json = @mysql_real_escape_string(htmlspecialchars($_GET["json"])); | |
$output = @mysql_real_escape_string(htmlspecialchars($_GET["output"])); | |
$opts = array('http' => | |
array( | |
'method' => 'GET', | |
'max_redirects' => '5', | |
'ignore_errors' => '1' | |
) | |
); | |
$context = stream_context_create($opts); | |
// header('Access-Control-Allow-Origin: *'); | |
header('Access-Control-Allow-Methods: GET'); | |
if ($max_age) { | |
header("Cache-Control: max-age=$max_age"); | |
}else{ | |
header("Cache-Control: max-age=600"); | |
} | |
try { | |
if ($url) { | |
$html= file_get_contents($url); | |
echo $html; | |
} elseif ($key || $key2) { | |
if($key){ // if key belong to old api | |
$K= $key; | |
$url = "https://docs.google.com/spreadsheet/pub?key=$key&single=true&gid=0&output=csv"; | |
}else{ // se agrega para la nueva modalidad de exportar la data de google | |
$K= $key2; | |
$url = "https://docs.google.com/spreadsheets/d/$key2/export?format=csv&id=$key2&gid=0"; | |
} | |
if(!ini_set('default_socket_timeout', 200)) echo "<!-- unable to change socket timeout -->"; | |
// $key = "0Au32Xh0iDeNddDFjV2V0REJ4YXpraDhwMHE2QWVXS1E"; // test | |
$header = NULL; | |
$spreadsheet_data = array(); | |
if (($handle = fopen($url, 'r', false, $context)) !== FALSE) { | |
if ($output == 'csv'){ | |
header("content-type:application/csv;charset=UTF-8"); | |
header("Content-Disposition:attachment;filename=\"GS-$K.csv\""); | |
echo file_get_contents($url); | |
exit(); | |
} | |
while (($data = fgetcsv($handle, 10000000, ",")) !== FALSE) { | |
if ($json || $output == 'json') { // JSON FORMAT | |
if(!$header) | |
$header = $data; | |
else | |
$spreadsheet_data[] = array_combine($header, $data); | |
}else{ // ARRAY FORMAT | |
$spreadsheet_data[]=$data; | |
} | |
} | |
fclose($handle); | |
echo json_encode($spreadsheet_data); | |
} | |
else | |
echo die("Problem reading csv"); | |
} else | |
echo "Debe definir una URL o una KEY de Google Spreadsheet"; | |
} catch (Exception $e) { | |
echo 'Excepción capturada: ', $e->getMessage(), "\n"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment