Archive

Archive for the ‘PHP + MySQL’ Category

Send email when medical results appear on lam24.pl

24/09/2015 Leave a comment

Bellow php script will check every 15 min if medical results appear on lam24.pl and send email with notification.


<?php
$url = "https://lam24.pl/wyniki.aspx&quot;;
$zlecenie = "xxxxxxxxx";
$pesel = "xxxxxxxxxxx";

$f = "lam24.ini";
$size = filesize($f);
$fH = fopen($f,"r");
$data = fread($fH,$size);
fclose($fH);
if(strpos($data,'true') !== false) {
exit();
}

$ckfile = tempnam("/tmp", "CURLCOOKIE");
$useragent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Safari/533.2';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

$html = curl_exec($ch);

curl_close($ch);

preg_match('~~', $html, $viewstate);
preg_match('~~', $html, $eventValidation);

$viewstate = $viewstate[1];
$eventValidation = $eventValidation[1];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

$postfields = array();
$postfields['__EVENTTARGET'] = "";
$postfields['__EVENTARGUMENT'] = "";
$postfields['__VIEWSTATE'] = $viewstate;
$postfields['__EVENTVALIDATION'] = $eventValidation;
$postfields['Login$UserName'] = $zlecenie;
$postfields['Login$Password'] = $pesel;
$postfields['Login$loginButton'] = 'Pokaż wyniki';

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$ret = curl_exec($ch);
if(strpos($ret,'Nie znaleziono wynik') !== true) {
$fp = fopen($f, 'w');
fwrite($fp, 'true');
fclose($fp);

$to = 'user@gmail.com';
$subject = 'Wyniki Lam24';
$message = 'Automatyczny skrypt, ktory sprawdza dostepnosc wynikow co 15 minut informuje za sa one juz dostepne:' . "\r\n" . 'https://lam24.pl/wyniki.aspx' . "\r\n" .
'Zlecenie: ' . $zlecenie . "\r\n" .
'Pesel: ' . $pesel;
$headers = 'From: user@domain.pl' . "\r\n" .
'Reply-To: user@domain.pl' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
}
curl_close($ch);
?>

crontab:
*/15 * * * * /usr/bin/php /home/n4p1/lam24/lam24.php

Advertisements
Categories: Bash, PHP + MySQL

Import XML data to MySQL tables

01/08/2012 1 comment

According to mysql documentation there is possibility to load data to mysql table from xml files. Hoverer after trying to execute below command:

napi@debian:/home/napi$ mysql -u root -p
mysql> use test;
Database changed
mysql> LOAD XML LOCAL INFILE '/var/www/SIMC.xml' INTO TABLE simc;
ERROR 1148 (42000): The used command is not allowed with this MySQL version

as a output I get this weird error. To fix that just add one parameter: ‘–local-infile’ to mysql command:

napi@debian:/home/napi$ mysql -u root -p --local-infile
mysql> use test;
Database changed
mysql>  LOAD XML LOCAL INFILE '/var/www/SIMC.xml' INTO TABLE simc;
Query OK, 4115 rows affected, 1580 warnings (0.85 sec)
Records: 4115  Deleted: 0  Skipped: 0  Warnings: 1580
Categories: PHP + MySQL

How to parse lotto numbers from liczby.pl and put it to DB?

21/07/2011 Leave a comment

Answers is:


<?php
$hostname = "localhost";
$username = "lotto";
$password = "";

try {
$db = new PDO("mysql:host=$hostname;dbname=lotto", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

$n = 1;
while ($n <= 126) {
$html = file_get_contents("http://www.liczby.pl/index.php?d=l&dl_d=wyniki_duzego_lotka&dl_w=plansza&w_dl_strona=$n");
preg_match_all("#(<td nowrap>(.*?)</td>)|(<td align=\"center\"  class=\"(.*?)\" title=\"(.*?)\">(.*?)</td>)#", $html, $match);
$i=0;

$match2 = array_mesh($match[2], $match[6]);

foreach ($match2 as $record){
if($i==7 or $i==0){
$time = strtotime($record);
$db->query("INSERT INTO losowania (id, data) VALUES ('', $time)");
$id = $db->lastInsertId();
if($i==7){
$i=0;
}
}
else {
$db->query("INSERT INTO liczby (id, losowania_id, liczby) VALUES ('', $id, $record)");
}
$i++;
}
$n++;
}
$db = null; // close the database connection
}
catch(PDOException $e) {
echo $e->getMessage();
}

function array_mesh() {
// Combine multiple associative arrays and sum the values for any common keys
// The function can accept any number of arrays as arguments
// The values must be numeric or the summed value will be 0

// Get the number of arguments being passed
$numargs = func_num_args();

// Save the arguments to an array
$arg_list = func_get_args();

// Create an array to hold the combined data
$out = array();

// Loop through each of the arguments
for ($i = 0; $i < $numargs; $i++) {
$in = $arg_list[$i]; // This will be equal to each array passed as an argument

// Loop through each of the arrays passed as arguments
foreach($in as $key => $value) {
// If the same key exists in the $out array
if(array_key_exists($key, $out)) {
// Sum the values of the common key
$sum = $in[$key].$out[$key];
// Add the key => value pair to array $out
$out[$key] = $sum;
}else{
// Add to $out any key => value pairs in the $in array that did not have a match in $out
$out[$key] = $in[$key];
}
}
}

return $out;
}
?>

Categories: PHP + MySQL, Programming