Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php] problemik
Forum PHP.pl > Forum > Gotowe rozwiązania
dragonik
mam plik rss_reader ktory czyta z plikow rss wszystko i potem wstawia w tablice $arr no a potem mozna sobie spokojnie wszystko wypisac na stronie

  1. <?php
  2. /* php RSS Reader v1.1
  3. By Richard James Kendall 
  4. Bugs to richard@richardjameskendall.com 
  5. Free to use, please acknowledge me 
  6.  
  7. Place the URL of an RSS feed in the $file variable.
  8.  
  9.  The $rss_channel array will be filled with data from the feed,
  10.  every RSS feed is different by by and large it should contain:
  11.  
  12.  Array {
  13.  [TITLE] = feed title
  14.  [DESCRIPTION] = feed description
  15.  [LINK] = link to their website
  16.  
  17.  [IMAGE] = Array {
  18.  [URL] = url of image
  19.  [DESCRIPTION] = alt text of image
  20.  }
  21.  
  22.  [ITEMS] = Array {
  23.  [0] = Array {
  24.  [TITLE] = item title
  25.  [DESCRIPTION] = item description
  26.  [LINK = a link to the story
  27.  }
  28.  .
  29.  .
  30.  .
  31.  }
  32.  }
  33.  
  34.  By default it retrives the Reuters Oddly Enough RSS feed. The data is put 
  35. nto the array
  36.  structure so you can format the information as you see fit.
  37. */
  38.  
  39. $file = &#092;"http://this-is-the-love-song.blog.pl/index.rss\";
  40.  
  41. $rss_channel = array();
  42. $currently_writing = &#092;"\";
  43. $main = &#092;"\";
  44. $item_counter = 1;
  45.  
  46. function startElement($parser, $name, $attrs) {
  47.  global $rss_channel, $currently_writing, $main;
  48.  switch($name) {
  49.  case &#092;"RSS\":
  50.  case &#092;"RDF:RDF\":
  51.  case &#092;"ITEMS\":
  52.  $currently_writing = &#092;"\";
  53.  break;
  54.  case &#092;"CHANNEL\":
  55.  $main = &#092;"CHANNEL\";
  56.  break;
  57.  case &#092;"IMAGE\":
  58.  $main = &#092;"IMAGE\";
  59.  $rss_channel[&#092;"IMAGE\"] = array();
  60.  break;
  61.  case &#092;"ITEM\":
  62.  $main = &#092;"ITEMS\";
  63.  break;
  64.  default:
  65.  $currently_writing = $name;
  66.  break;
  67.  }
  68. }
  69.  
  70. function endElement($parser, $name) {
  71.  global $rss_channel, $currently_writing, $item_counter;
  72.  $currently_writing = &#092;"\";
  73.  if ($name == &#092;"ITEM\") {
  74.  $item_counter++;
  75.  }
  76. }
  77.  
  78. function characterData($parser, $data) {
  79. global $rss_channel, $currently_writing, $main, $item_counter;
  80. if ($currently_writing != &#092;"\") {
  81. switch($main) {
  82. case &#092;"CHANNEL\":
  83. if (isset($rss_channel[$currently_writing])) {
  84. $rss_channel[$currently_writing] .= $data;
  85. } else {
  86. $rss_channel[$currently_writing] = $data;
  87. }
  88. break;
  89. case &#092;"IMAGE\":
  90. if (isset($rss_channel[$main][$currently_writing])) {
  91. $rss_channel[$main][$currently_writing] .= $data;
  92. } else {
  93. $rss_channel[$main][$currently_writing] = $data;
  94. }
  95. break;
  96. case &#092;"ITEMS\":
  97. if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
  98. $rss_channel[$main][$item_counter][$currently_writing] .= $data;
  99. } else {
  100. //print (\"rss_channel[$main][$item_counter][$currently_writing] = $data<br>\");
  101. $rss_channel[$main][$item_counter][$currently_writing] = $data;
  102. }
  103. break;
  104. }
  105. }
  106. }
  107.  
  108. $xml_parser = xml_parser_create();
  109. xml_set_element_handler($xml_parser, &#092;"startElement\", \"endElement\");
  110. xml_set_character_data_handler($xml_parser, &#092;"characterData\");
  111. if (!($fp = fopen($file, &#092;"r\"))) {
  112. die(&#092;"could not open XML input\");
  113. }
  114.  
  115. while ($data = fread($fp, 4096)) {
  116. if (!xml_parse($xml_parser, $data, feof($fp))) {
  117. die(sprintf(&#092;"XML error: %s at line %d\",
  118. xml_error_string(xml_get_error_code($xml_parser)),
  119. xml_get_current_line_number($xml_parser)));
  120. }
  121. }
  122. xml_parser_free($xml_parser);
  123.  
  124. ?>


w tym pliku podaje sie $file czyli jaki plik ma czytac

na swojej stronie includuje ten plik i potem wypisuje tytul ostatniej notki np
echo $rss_channel["TITLE"];

ale co jezeli mam wiecej stron do wczytania? zrobilem pare plikow rss_reader(2,3,4), do tego musialem jeszcze zmienic nazwy funkcji.. to raczej jest zle rozwiazanie.

co zrobic zeby potrzebny byl tylko jeden plik do zincludowania i potem spokojnie mozna by bylo czytac z roznych zrodel.

Pozdrowienia,
Dragonik
tiraeth
Zamień to na klasę smile.gif później dodajesz w klasie funkcję np. close_rss() i znowu jedziesz klasa na inny plik smile.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.