Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: nixelce
- * Date: 21.10.2017
- * Time: 14:09
- */
- $table_name = 'testtable';
- $link = pg_pconnect("host=localhost port=5432 dbname=testDB user=postgres password=")
- or die("Ошибка " . pg_errormessage());
- $query ='SELECT * FROM '. $table_name;
- $result = pg_query($link, $query) or die("Ошибка " . pg_errormessage($link));
- while ($row = pg_fetch_assoc($result)) {
- $name = $row['name'];
- $rate = $row['rate'];
- $date = $row['date'];
- print ($name." ");
- print ($rate ." ");
- print ($row . "\n");
- }
- $rss = "http://www.nationalbank.kz/rss/rates_all.xml";
- $xmlstr = @file_get_contents($rss);
- if($xmlstr===false)die('Error connect to RSS: '.$rss);
- $xml = new SimpleXMLElement($xmlstr);
- if($xml===false)die('Error parse RSS: '.$rss);
- $pubDate = DateTime::createFromFormat('d.m.y', (string)$xml->channel->item->pubDate);
- $pubDate = $pubDate->format('Y-m-d');
- $result = pg_query($link, 'SELECT * FROM ' . $table_name . ' WHERE date = \'' . $pubDate . '\'');
- if (!pg_num_rows($result))
- {
- foreach ($xml->channel->item as $item)
- {
- $pubDate = DateTime::createFromFormat('d.m.y', (string)$item->pubDate);
- $pubDate = $pubDate->format('Y-m-d');
- $query = 'INSERT INTO ' . $table_name . '(date, name, rate) VALUES (\'' . $pubDate . '\', \'' . (string)$item->title . '\', ' . (double)$item->description . ')';
- $ins = pg_query($link, $query);
- }
- }
- else print("Данные за этот день уже загружены\n");
- if (pg_close())
- {
- print ("Соединение закрыто");
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement