Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: nixelce
  5. * Date: 21.10.2017
  6. * Time: 14:09
  7. */
  8. $table_name = 'testtable';
  9. $link = pg_pconnect("host=localhost port=5432 dbname=testDB user=postgres password=")
  10. or die("Ошибка " . pg_errormessage());
  11.  
  12. $query ='SELECT * FROM '. $table_name;
  13. $result = pg_query($link, $query) or die("Ошибка " . pg_errormessage($link));
  14.  
  15. while ($row = pg_fetch_assoc($result)) {
  16. $name = $row['name'];
  17. $rate = $row['rate'];
  18. $date = $row['date'];
  19. print ($name." ");
  20. print ($rate ." ");
  21. print ($row . "\n");
  22. }
  23.  
  24. $rss = "http://www.nationalbank.kz/rss/rates_all.xml";
  25.  
  26. $xmlstr = @file_get_contents($rss);
  27. if($xmlstr===false)die('Error connect to RSS: '.$rss);
  28. $xml = new SimpleXMLElement($xmlstr);
  29. if($xml===false)die('Error parse RSS: '.$rss);
  30.  
  31. $pubDate = DateTime::createFromFormat('d.m.y', (string)$xml->channel->item->pubDate);
  32. $pubDate = $pubDate->format('Y-m-d');
  33.  
  34. $result = pg_query($link, 'SELECT * FROM ' . $table_name . ' WHERE date = \'' . $pubDate . '\'');
  35.  
  36. if (!pg_num_rows($result))
  37. {
  38. foreach ($xml->channel->item as $item)
  39. {
  40. $pubDate = DateTime::createFromFormat('d.m.y', (string)$item->pubDate);
  41. $pubDate = $pubDate->format('Y-m-d');
  42. $query = 'INSERT INTO ' . $table_name . '(date, name, rate) VALUES (\'' . $pubDate . '\', \'' . (string)$item->title . '\', ' . (double)$item->description . ')';
  43. $ins = pg_query($link, $query);
  44. }
  45. }
  46. else print("Данные за этот день уже загружены\n");
  47. if (pg_close())
  48. {
  49. print ("Соединение закрыто");
  50. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement