Last active
January 7, 2016 16:18
-
-
Save donamkhanh/f827b130966434e09051 to your computer and use it in GitHub Desktop.
Extract lottery number of xskt.com.vn from 01/01/2015 to present
This file contains 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 | |
date_default_timezone_set('Asia/Ho_Chi_Minh'); | |
include_once __DIR__ . '/simple_html_dom.php'; | |
// Connecting, selecting database | |
$host = '127.0.0.1'; | |
$user = 'root'; | |
$pass = 'root'; | |
$dbName = 'lottery'; | |
$db = new mysqli($host, $user, $pass, $dbName); | |
/* check connection */ | |
if ($db->connect_errno) { | |
printf("Connect failed: %s\n", $db->connect_error); | |
exit(); | |
} | |
/* create a prepared statement */ | |
$stmt = $db->stmt_init(); | |
function getLottery($date) { | |
global $db, $stmt; | |
$html = file_get_html('http://xskt.com.vn/ket-qua-xo-so-theo-ngay/mien-bac-xsmb/' . $date . '.html'); | |
$row = $html->find('table.result tr', 1); | |
if (! $row) { | |
return; | |
} | |
$number = $row->find('td', 1); | |
if (! $number) { | |
return; | |
} | |
$number = substr($number->plaintext, -2); | |
$formattedDate = explode('-', $date); | |
$formattedDate = array_reverse($formattedDate); | |
$formattedDate = implode('-', $formattedDate); | |
$stmt->prepare("INSERT INTO numbers (`date`, `number`) VALUES (?, ?)"); | |
$stmt->bind_param('ss', $formattedDate, $number); | |
if (! $stmt->execute()) { | |
printf("Error: %s\n", $db->error); | |
return; | |
} | |
return $number; | |
} | |
$date = '31-12-2009'; | |
$endDate = date('d-m-Y'); | |
while ($date != $endDate) { | |
$date = date('d-m-Y', strtotime("$date +1 day")); | |
printf("Fetching data from %s. The number is %s\n", $date, getLottery($date)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment