An example using php-curl to get the latest Mega-Sena game result directly from the official site.
The script gets the data from the official website where it is necessary to activate the cookie. It is therefore necessary to set CURLOPT_COOKIESESSION
, CURLOPT_COOKIEFILE
and CURLOPT_COOKIEJAR
, without which the redirect token does not load the page.
The CURLOPT_FOLLOWLOCATION
parameter must be true
to allow redirection.
Parameter CURLOPT_RETURNTRANSFER
as true
so that the result is not dispatched directly to the browser, so you can manipulate the received string.
$c = curl_init();
$cookie_file = __DIR__.DIRECTORY_SEPARATOR.'megasena.txt';
curl_setopt_array($c, array(
CURLOPT_URL => 'http://www.loterias.caixa.gov.br/wps/portal/loterias/landing/megasena',
CURLOPT_REFERER => 'http://www.loterias.caixa.gov.br',
CURLOPT_USERAGENT => 'Foo Spider',
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 6,
CURLOPT_TIMEOUT => 6,
CURLOPT_MAXREDIRS => 1,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEFILE => $cookie_file,
CURLOPT_COOKIEJAR => $cookie_file
));
try {
$content = curl_exec($c);
$data = curl_getinfo($c);
$data['content'] = $content;
unset($content);
$data['errno'] = curl_errno($c);
$data['errmsg'] = curl_error($c);
if ((int)$data['errno'] !== 0 || (int)$data['http_code'] !== 200) {
echo 'error number: '.$data['errno'];
echo 'error message: '.$data['errmsg'];
echo 'http status: '.$data['http_code'];
//print_r($data);
exit;
}
} catch (HttpException $ex) {
print_r($ex); exit;
}
curl_close($c);
$doc = new DOMDocument();
@$doc->loadHTML($data['content']);
unset($data);
$tags = $doc->getElementsByTagName('ul');
$data = null;
foreach ($tags as $tag) {
if ($tag->getAttribute('class') == 'numbers mega-sena') {
$data = trim($tag->textContent);
break;
}
}
$arr = str_split($data, 2);
print_r($arr);
The result of the game is a <ul>
element whose class
is numbers mega-sena
.
The logic is just to extract what matters, by iterating the object $tags
until it finds the target.
The end result will be only the numbers. Example:
304247505558
I used str_split () to separate each ten into an array, which returns this:
Array
(
[0] => 30
[1] => 42
[2] => 47
[3] => 50
[4] => 55
[5] => 58
)
Note: The game numbers are from Contest 1795 (3/2/2016).
For results from previous games, follow the logic suggested in the @ Caffé comment. .
At the bottom of the page of the results of each type of game (megasena,
lotofácil ...) Caixa provides a file with the result of
all games: Examples:
loterias.caixa.gov.br/wps/portal/loterias/landing/megasena e
loterias.caixa.gov.br/wps/portal/loterias/landing/lotofacil. You can
make an initial load of these files for your database, and go
updating the base using the response API or even loading
differential of such files. - Caffé 03/03 at 16:26
Please be aware that the official website does not provide, at least, an adequate way to get the results of the games.
That's the best you can do. A gambiarra.
If you want to extract other data, for example the contest number, prize amount, etc., just read the HTML code generated by the target page. Then create routines to abstract the data you want, as the example demonstrates the abstraction of the number drawn.
I stress that the script is an example with didactic purpose. The try/catch
snippet, such as the snippet that identifies error return and the end result, implements as appropriate for your case.
Important to be aware that a change in the results page codes can affect the operation. Therefore, you should always be aware of any changes on the page from which you obtain the data.
For other games like Easy Lotus, follow the same logic as example.