Last active
May 26, 2020 22:56
-
-
Save Ruekompa/8d906cf1aa94ce1b31a2c0287ece91a1 to your computer and use it in GitHub Desktop.
This file contains 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 | |
require "config.php"; | |
// existing code to fetch numbers here | |
try { | |
$connection = new PDO($dsn, $username, $password, $options); | |
} | |
catch(PDOException $error) { | |
echo "Cannot connect to database. Fix that." . "<br>" . $error->getMessage(); | |
logfile($error->getMessage()); | |
} | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'https://coronavirus-tracker-api.herokuapp.com/v2/locations/665?source=nyt&timelines=false'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); | |
$headers = array(); | |
$headers[] = 'Accept: application/json'; | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
$result = curl_exec($ch); | |
$json = json_decode($result, true); | |
$table = "hernando"; | |
$confirmed = $json['location']['latest']['confirmed']; | |
$deaths = $json['location']['latest']['deaths']; | |
if (curl_errno($ch)) { | |
echo 'Error:' . curl_error($ch); | |
} else { | |
// This is simplified | |
$sql = sprintf( | |
"INSERT INTO %s (%s) values (%d, %d)", | |
$table, "confirmed , deaths", $confirmed, $deaths | |
); | |
try { | |
$statement = $connection->prepare($sql); | |
$statement->execute($ret); | |
} | |
catch(PDOException $error) { | |
echo $sql . "<br>" . $error->getMessage(); | |
logfile($error->getMessage()); | |
} | |
} | |
curl_close($ch); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment