How can I get the results of the lotteries?


I looked for lottery APIs to get the results of the contests, unfortunately I could not find any.

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

asked by anonymous 20.01.2015 / 16:02

2 answers


EDIT (08/29/2016): This API was taken from the air

As mentioned by the Fernando an API to see the lottery results can be seen here .

Just make a GET to and parse the JSON.


If allow_url_fopen is active

$json = json_decode(file_get_contents(''));

If it is not

$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_URL, '');
curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);

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

jQuery with Ajax

    url: "",
    dataType: "text",
    success: function(data) {
        var json = $.parseJSON(data);
20.01.2015 / 16:43

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 => '',
    CURLOPT_USERAGENT => 'Foo Spider',
    CURLOPT_HEADER => true,
    CURLOPT_COOKIEFILE => $cookie_file,
    CURLOPT_COOKIEJAR => $cookie_file

try {
    $content = curl_exec($c);
    $data = curl_getinfo($c);
    $data['content'] = $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'];
} catch (HttpException $ex) {
    print_r($ex); exit;


$doc = new DOMDocument();
$tags = $doc->getElementsByTagName('ul');
$data = null;
foreach ($tags as $tag) {
    if ($tag->getAttribute('class') == 'numbers mega-sena') {
        $data = trim($tag->textContent);
$arr = str_split($data, 2);

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:


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

    [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: e 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.

05.03.2016 / 20:08