Last active
October 26, 2022 15:07
-
-
Save tsprates/54f4413748f78795de801bc256189b8b to your computer and use it in GitHub Desktop.
Script to check Brazil's Election for President 2022
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 | |
// Brazil's election for president 2022 | |
// Author: Thiago Prates | |
$url = 'https://resultados.tse.jus.br/oficial/ele2022/544/dados-simplificados/br/br-c0001-e000544-r.json'; | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
// Link: https://curl.se/docs/caextract.html | |
// curl_setopt($ch, CURLOPT_CAINFO, './cacert.pem'); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$data = curl_exec($ch); | |
curl_close($ch); | |
if (curl_errno($ch) > 0) { | |
echo curl_error($ch) . PHP_EOL; | |
exit(); | |
} | |
echo str_pad('Candidato', 21) . 'Pct. (%)' . PHP_EOL; | |
$json = json_decode($data, true); | |
foreach ($json['cand'] as $cand) { | |
$name = html_entity_decode($cand['nm'], ENT_QUOTES | ENT_HTML5); | |
$name = ucwords(mb_strtolower($name)); | |
echo $name . str_repeat('.', 21 - mb_strlen($name)) . $cand['pvap'] . '%' . PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment