How can I get the lottery results?

Asked

Viewed 16,859 times

14

I searched for lottery API’s to get the results of the contests, unfortunately I could not find any.

The response of @fpg1503 can read only the result of the current game, I wonder if it is possible to get the results of previous games too.

  • Has that, helping?

  • Diponibilizo todos os resultados em http://loteria.albertino.eti.br

  • 2

    Very good question. I did not understand why the negatives. + 1.

  • 5

    At the bottom of the results page of each game type (megasena, Easy Otoeasy...) Box provides a file with the result of all games: Examples: http://www.loterias.caixa.gov.br/wps/portal/loterias/landing/megasena and http://www.loterias.box.gov.br/wps/portal/lotteries/Landing/lotofacil. You can make an initial load of these files to your database, and go updating the base using the response API or making differential load of such files.

  • 3

    @Caffé the idea is good, but they send a boring HTML inside this zip. You would have to improvise a Crawler to get the data out, or do some other "stunt". But at least it’s official.

  • 1

    Four years after the question and the Cashier has not yet made available an official API for obtaining lottery results. I made a classlybrary in C# some time ago and I already needed to modify several times by fetal changes on the site. I don’t know if she’s still working, but the link to her is here. I know it is not the best option (I program for a hobby), but at the time it served my goals. Tomorrow I will do some tests, if you have time. If it’s not working anymore I’ll try to fix it.

Show 1 more comment

2 answers

15


EDIT (08/29/2016): This API has been taken off the air

As mentioned by Fernando an API to see the result of lotteries can be seen here.

Just make a GET for http://developers.agenciaideias.com.br/loterias/loteriafederal/json and parse the JSON.

PHP

If allow_url_fopen is active

$json = json_decode(file_get_contents('http://developers.agenciaideias.com.br/loterias/loteriafederal/json'));

If you’re not

$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_URL, 'http://developers.agenciaideias.com.br/loterias/loteriafederal/json');
curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);

$json = json_decode(curl_exec($curlSession));
curl_close($curlSession);

jQuery with Ajax

$.ajax({
    url: "http://developers.agenciaideias.com.br/loterias/loteriafederal/json",
    dataType: "text",
    success: function(data) {
        var json = $.parseJSON(data);
    }
});
  • 1

    I don’t understand why the -1

  • 2

    We are at the end of August, 7 months after you posted. I waited this time to answer the reason for the negatives. You can see that the source of your code query no longer exists. This was because it was not an official source but only a third source with no guarantee of maintenance. At the time I did not negatively. But now I give a negative. I wasn’t hoping to happen. I just waited for the obvious... 404 not found

13

An example using php-Curl to get the result of the latest Mega-Sena game, directly from the official website.

The script retrieves the data from the official website where it is necessary to activate the cookie. That is why it is necessary to set CURLOPT_COOKIESESSION, CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR, without which, the redirecting token does not load the page.

The parameter CURLOPT_FOLLOWLOCATION needs to look like true to allow redirection.

The Parameter CURLOPT_RETURNTRANSFER as true so that the result is not dispatched directly in the browser, thus being able to 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 in an element <ul> whose class is numbers mega-sena.

The logic is just to extract what matters by iterating the object $tags until I find the target.

The final result will only be the numbers. Example:

304247505558

I used str_split() to separate each dozen into an array, which returns this:

Array
(
    [0] => 30
    [1] => 42
    [2] => 47
    [3] => 50
    [4] => 55
    [5] => 58
)

Note: Game numbers are from the 1795 Contest (02/03/2016).

To get the results of previous games, follow the suggested logic in the @Caffè.

At the bottom of the results page of each type of game (megasena, Easy Otoeasy...) Box provides a file with the result of all the games: Examples: lotteries.caixa.Gov.br/wps/portal/lotteries/Landing/megasena and loterias.caixa.Gov.br/wps/portal/lotteries/Landing/lotofacil. You can make an initial load of these files to your database, and go updating the base using the response API or still loading differentiator of such files. - Caffé 2/03 at 16:26

Be aware that the official website does not provide, at least I do not know, an appropriate way to get the results of the games.

That’s the most you can do. A gambit.

If you want to extract other data, for example, the contest number, prize value, 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 drawn game number.

I emphasize that the script is an example with didactic purpose. The excerpt with try/catch such as the section that identifies error return and the final result, implement as appropriate for your case.

It is important to be aware that a change in the results page codes can affect the functioning. So you should always keep an eye out for any changes to the page from which you get the data.

For other games like Easy Lotto, follow the same logic as the example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.