Skip to content

Instantly share code, notes, and snippets.

@wnasich
Last active August 30, 2020 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wnasich/c53739a08739ab09d5fe37625ed8f851 to your computer and use it in GitHub Desktop.
Save wnasich/c53739a08739ab09d5fe37625ed8f851 to your computer and use it in GitHub Desktop.
[INTA hizo cambios en su sitio. Este código no funciona] Obtención de datos históricos meteorológicos desde http://siga2.inta.gov.ar/en/datoshistoricos/
<?php
class IntaWeather {
public $endPointUrl = 'http://siga2.inta.gov.ar/en/datoshistoricos/getDatosTabla';
// Locations and fields scrapped from http://siga2.inta.gov.ar/en/datoshistoricos/
public $locations = [
'eea-reconquista-reconquista' => ['id' => 294, 'idP' => 55],
'eea-oliveros-villa-cañas' => ['id' => 299, 'idP' => 55],
];
// Order of elements is important. Response will content data in site predefined order.
// Keep same order than displayed on the site
public $fields = [
'PrecDiaCrono',
'TempAbrigo150Max',
'TempAbrigo150Min',
];
public $block = 80; // days to request
function executeHttpRequest($fromTime, array $params) {
$postData = [
'id' => $params['id'],
'idP' => $params['idP'],
'T' => 'False',
];
$postData['vars'] = join(',', $this->fields);
$fromDate = DateTime::createFromFormat('Y-m-d|+', $fromTime);
$fromDate->add(New DateInterval('P1D'));
$postData['fi'] = $fromDate->format('j/m/Y');
$fromDate->add(New DateInterval('P' . $this->block . 'D'));
$nowDate = DateTime::createFromFormat('Y-m-d|', date('Y-m-d'));
if ($fromDate >= $nowDate) {
$fromDate = $nowDate;
}
$postData['ff'] = $fromDate->format('j/m/Y');
$curlOptions = [
CURLOPT_URL => $this->endPointUrl,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 120,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($postData),
CURLOPT_HTTPHEADER => ['X-Requested-With: XMLHttpRequest'],
];
$ch = curl_init();
curl_setopt_array($ch, $curlOptions);
$response = curl_exec($ch);
if ($response === false) {
$curlError = curl_error($ch);
throw new RuntimeException('Error while performing curl: ' . $curlError);
}
curl_close($ch);
$jsonData = json_decode($response, true);
if (!$jsonData or !isset($jsonData['aaData'])) {
echo "Invalid jsonData for postData " . print_r($postData, true);
}
$rows = [];
foreach ($jsonData['aaData'] as $weatherData) {
$rowDate = DateTime::createFromFormat('j/m/Y|', $weatherData[0]);
$index = 1;
$row = ['datetime' => $rowDate];
foreach ($this->fields as $field) {
$row[$field] = $weatherData[$index];
$index++;
}
$rows[] = $row;
}
return $rows;
}
}
// Usage
$weather = new IntaWeather();
$fromTime = date('Y-m-d H:i:s', strtotime('-3 days'));
foreach ($weather->locations as $name => $params) {
$rows = $weather->executeHttpRequest($fromTime, $params);
echo 'Weather conditions at : ' . $name;
print_r($rows);
echo "-----\n\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment