Current Path : /data/web/virtuals/51568/virtual/www/class/ |
Current File : /data/web/virtuals/51568/virtual/www/class/stahovacJidelnicku.class.php |
<?php require_once "pdoDB.class.php"; $pdo = new pdoDB(); class StahovacJidelnicku{ private $url; function __construct($url){ $this->url = $url; } function run(){ $xmlString = $this->downloadContent(); $xml = $this->parseXML($xmlString); $this->deleteOldRecords(); $this->insertNewRecords($xml); } private function downloadContent(){ $document = $this->get_fcontent($this->url); return $document[0]; } private function parseXML($xmlString){ $xml = simplexml_load_string($xmlString); if ($xml === false) { echo "Failed loading XML: "; foreach(libxml_get_errors() as $error) { echo "<br>", $error->message; } //exit(); } else { return $xml; } } private function deleteOldRecords(){ global $pdo; $pdo->execute("delete from jidelnicek where id > :id", array(':id' => 0)); } private function insertNewRecords($xmlData){ global $pdo; foreach($xmlData as $item){ $pdo->execute("insert into jidelnicek set datum = :datum, druh = :druh, druh_popis = :druh_popis, nazev = :nazev, popis = :popis, popis_al = :popis_al, popis_alzk = :popis_alzk", array(':datum' => $item->datum, ':druh' => $item->druh, ':druh_popis' => $item->druh_popis, ':nazev' => $item->nazev, ':popis' => $item->popis, ':popis_al' => $item->popis_al, ':popis_alzk' => $item->popis_alzk)); } } private function get_fcontent( $url, $javascript_loop = 0, $timeout = 20 ) { $url = str_replace( "&", "&", urldecode(trim($url)) ); $cookie = tempnam ("tmp", "CURLCOOKIE"); $ch = curl_init(); curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" ); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie ); curl_setopt( $ch, CURLOPT_ENCODING, "" ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch, CURLOPT_AUTOREFERER, true ); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); # required for https urls curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout ); curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout ); curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 ); $content = curl_exec( $ch ); $response = curl_getinfo( $ch ); curl_close ( $ch ); if ($response['http_code'] == 301 || $response['http_code'] == 302) { ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1"); if ( $headers = get_headers($response['url']) ) { foreach( $headers as $value ) { if ( substr( strtolower($value), 0, 9 ) == "location:" ) return get_url( trim( substr( $value, 9, strlen($value) ) ) ); } } } if ( ( preg_match("/>[[:space:]]+window\.location\.replace\('(.*)'\)/i", $content, $value) || preg_match("/>[[:space:]]+window\.location\=\"(.*)\"/i", $content, $value) ) && $javascript_loop < 5) { return get_url( $value[1], $javascript_loop+1 ); } else { return array( $content, $response ); } } }