Skip to content

Instantly share code, notes, and snippets.

@tsprates
Last active October 26, 2022 15:07
Show Gist options
  • Save tsprates/54f4413748f78795de801bc256189b8b to your computer and use it in GitHub Desktop.
Save tsprates/54f4413748f78795de801bc256189b8b to your computer and use it in GitHub Desktop.
Script to check Brazil's Election for President 2022
<?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