Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php][xml]Czytnik rss z zapisywaniem do bazy danych
Forum PHP.pl > Forum > PHP
Skicek
Witam,
mam skrypt zczytujacy rss lecz nie zczytuje mi daty wpisu nie wiem czemu. A jest mi ona potrzebna do sprawdzenia czy dany wpis jest juz w bazie danych czy tez nie.


pomocnicze funkcje
  1. <?php
  2. require_once 'Entry.php';
  3.  
  4. class Channel {
  5. function __construct($url, $title, $desc) {
  6. $this->url = $url;
  7. $this->title = $title;
  8. $this->description = $desc;
  9. }
  10.  
  11. public function getURL() {
  12. return $this->url;
  13. }
  14.  
  15. public function getTitle() {
  16. return $this->title;
  17. }
  18.  
  19. public function getDescription() {
  20. return $this->description;
  21. }
  22.  
  23. public function addEntry(Entry $e) {
  24. $this->entries[] = $e;
  25. }
  26.  
  27. public function getEntries() {
  28. return $this->entries;
  29. }
  30.  
  31. private $url;
  32. private $title;
  33. private $description;
  34. private $entries = array();
  35. }
  36. ?>



pomocnicze funkcje
  1. <?php
  2. require_once 'Channel.php';
  3.  
  4. class Entry {
  5. public function __construct(Channel $c, $url, $title,
  6. $date, $desc) {
  7. $this->channel = $c;
  8. $this->url = $url;
  9. $this->title = $title;
  10. $this->date = $date;
  11. $this->description = $desc;
  12. }
  13.  
  14. public function getChannel() {
  15. return $this->channel;
  16. }
  17.  
  18. public function getURL() {
  19. return $this->url;
  20. }
  21.  
  22. public function getTitle() {
  23. return $this->title;
  24. }
  25.  
  26. public function getDate() {
  27. return $this->date;
  28. }
  29.  
  30. public function getDescription() {
  31. return $this->description;
  32. }
  33.  
  34. private $channel;
  35. private $url;
  36. private $title;
  37. private $date;
  38. private $description;
  39. }
  40. ?>



wykonywalna czesc
  1. <?php
  2. require_once 'Entry.php';
  3. require_once 'Channel.php';
  4.  
  5. class Channels {
  6. public static function user($user) {
  7. $url = 'http://wiadomosci.wp.pl/kat,1342,ver,rss,rss.xml';
  8. self::$urls[] = $url;
  9. }
  10.  
  11. public static function get() {
  12. $mh = curl_multi_init();
  13. $chs = array();
  14. $channels = array();
  15. $active = 0;
  16.  
  17.  
  18. foreach (self::$urls as $url) {
  19. $ch = curl_init($url);
  20. $chs[] = $ch;
  21.  
  22. curl_setopt_array($ch, self::$options);
  23. curl_multi_add_handle($mh, $ch);
  24. }
  25.  
  26. do {
  27. $result = curl_multi_exec($mh, $active);
  28. } while ($result == CURLM_CALL_MULTI_PERFORM);
  29.  
  30. do {
  31. if (curl_multi_select($mh) != -1) {
  32. do {
  33. $result = curl_multi_exec($mh, $active);
  34. } while ($result == CURLM_CALL_MULTI_PERFORM);
  35. }
  36. } while ($active && $result == CURLM_OK);
  37.  
  38. foreach ($chs as $ch) {
  39. if (curl_errno($ch) == 0) {
  40. $content = curl_multi_getcontent($ch);
  41. $channels[] = self::parseXML($content);
  42. }
  43. }
  44. return $channels;
  45. }
  46.  
  47. private static function parseXML($xml) {
  48. $connection = @mysql_connect('localhost','root','')
  49. or die('Brak połączenia z serwerem MySQL');
  50. $db = @mysql_select_db('adv', $connection)
  51. or die('Nie mogę połączyć się z bazą danych');
  52.  
  53. $root = new SimpleXMLElement($xml);
  54.  
  55. $url = $root->channel->link;
  56. $title = $root->channel->title;
  57. $description = $root->channel->description;
  58.  
  59. $channel = new Channel($url, $title, $description);
  60. $b=1;
  61. foreach ($root->channel->item as $item) {
  62.  
  63. $entryUrl = $item->link;
  64. $entryTitle = $item->title;
  65. $entryDate = strtotime($item->date);
  66. $entryDescription = $item->description;
  67.  
  68. /* $d1=addslashes($entryTitle);
  69. $d2=addslashes($entryDescription);
  70. if (last item w db == )
  71. $blue = "INSERT INTO dane(name, title, dane) VALUES ('skicek', '$d1', '$d2')";
  72. $ins = @mysql_query($blue);
  73. if($ins) echo $b." ok<br>";
  74. else echo $b." Błąd nie udało się dodać nowego rekordu<br>"; */
  75.  
  76. $entry = new Entry($channel, $entryUrl,
  77. $entryTitle, $entryDate,
  78. $entryDescription);
  79. $channel->addEntry($entry);
  80. $b=$b+1;
  81. }
  82. mysql_close($connection);
  83. return $channel;
  84. }
  85.  
  86. private static $urls;
  87. private static $options = array(CURLOPT_HEADER => false,
  88. CURLOPT_RETURNTRANSFER => true);
  89. }
  90.  
  91.  
  92. ?>



plik glowny
  1. <?php header('Content-Type: text/html; charset=UTF-8'); ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl">
  4. <head>
  5. <link href="style.css" rel="stylesheet" type="text/css" />
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <title>Czytnik RSS</title>
  8. </head>
  9. <body>
  10. <?php
  11.  
  12. require_once 'Channel.php';
  13. require_once 'Entry.php';
  14. require_once 'Channels.php';
  15.  
  16. Channels::user('skicek');
  17.  
  18. $channels = Channels::get();
  19.  
  20. ?>
  21. <?php foreach ($channels as $channel): ?>
  22. <div class="channel">
  23. <h1><a href="<?php echo $channel->getURL(); ?>"><?php echo $channel->getTitle(); ?></a></h1>
  24. <?php
  25. $entries = $channel->getEntries();
  26. foreach ($entries as $entry) :
  27. ?>
  28. <div class="entry">
  29. <h3><?php echo $entry->getTitle(); ?></h3>
  30. <h3><?php echo $entry->getDate(); ?></h3>
  31. <p>
  32. <?php echo $entry->getDescription(); ?>
  33. </p>
  34. </div>
  35. <?php endforeach; ?>
  36. </div>
  37. <?php endforeach; ?>
  38.  
  39. </body>
  40. </html>



wp dalem jako przykladowy xml.



ktos pomoże?
Tajgeer
W części wykonywalnej zmień:
  1. $entryDate = strtotime($item->date);

na
  1. $entryDate = strtotime($item->pubDate);
Skicek
dzieki faktycznie nie zauwazylem tego... tongue.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.