Pomoc - Szukaj - U¿ytkownicy - Kalendarz
Pe³na wersja: Potrzebna pomoc przy czytniku RSS na stronê WWW
Forum PHP.pl > Forum > Gotowe rozwi±zania
Spyder
Proszê o pomoc w uruchomieniu tej klasy ogólnie nie wiem jak siê za ni± zabrac dopisa³em na koñcu echo(Get("http://www.motoflesz.pl/rss.php")); ale to nie zdaje egzaminu potrzebuje tej klasy do sporego serwisu motoryzacyjnego w zamian moge umie¶ciæ link do strony lub benerek z podziêkowaniami za pomoc.
Chcê odczytaæ kana³ rss z danej strony w przyk³adzie http://www.motoflesz.pl/rss.php i nie wiem do koñca jak to mam zmieniæ ¿eby ruszy³o. Proszê o pomoc...

POZDRO dry.gif



  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <title>Untitled Document</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. <?php
  7. /*
  8.  ======================================================================
  9.  lastRSS 0.9.1
  10.  
  11.  Simple yet powerfull php class to parse RSS files.
  12.  
  13.  by Vojtech Semecky, webmaster @ webdot . cz
  14.  
  15.  Latest version, features, manual and examples:
  16.  <a href="http://lastrss.webdot.cz/" target="_blank">http://lastrss.webdot.cz/</a>
  17.  
  18.  ----------------------------------------------------------------------
  19.  LICENSE
  20.  
  21.  This program is free software; you can redistribute it and/or
  22.  modify it under the terms of the GNU General Public License (GPL)
  23.  as published by the Free Software Foundation; either version 2
  24.  of the License, or (at your option) any later version.
  25.  
  26.  This program is distributed in the hope that it will be useful,
  27.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  28.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29.  GNU General Public License for more details.
  30.  
  31.  To read the license please visit <a href="http://www.gnu.org/copyleft/gpl.html" target="_blank">http://www.gnu.org/copyleft/gpl.html</a>
  32.  ======================================================================
  33. */
  34.  
  35. /**
  36. * lastRSS
  37. * Simple yet powerfull php class to parse RSS files.
  38. */
  39. class lastRSS {
  40. // -------------------------------------------------------------------
  41. // Public properties
  42. // -------------------------------------------------------------------
  43. var $default_cp = 'UTF-8';
  44. var $CDATA = 'nochange';
  45. var $cp = '';
  46. var $items_limit = 0;
  47. var $stripHTML = False;
  48. var $date_format = '';
  49.  
  50. // -------------------------------------------------------------------
  51. // Private variables
  52. // -------------------------------------------------------------------
  53. var $channeltags = array ('title', 'link', 'description', 'language', 'copyright', 'managingEditor', 'webMaster', 'lastBuildDate', 'rating', 'docs');
  54. var $itemtags = array('title', 'link', 'description', 'author', 'category', 'comments', 'enclosure', 'guid', 'pubDate', 'source');
  55. var $imagetags = array('title', 'url', 'link', 'width', 'height');
  56. var $textinputtags = array('title', 'description', 'name', 'link');
  57.  
  58. // -------------------------------------------------------------------
  59. // Parse RSS file and returns associative array.
  60. // -------------------------------------------------------------------
  61. function Get ($rss_url) {
  62. // If CACHE ENABLED
  63. if ($this->cache_dir != '') {
  64. $cache_file = $this->cache_dir . '/rsscache_' . md5($rss_url);
  65. $timedif = @(time() - filemtime($cache_file));
  66. if ($timedif < $this->cache_time) {
  67. // cached file is fresh enough, return cached array
  68. $result = unserialize(join('', file($cache_file)));
  69. // set 'cached' to 1 only if cached file is correct
  70. if ($result) $result['cached'] = 1;
  71. } else {
  72. // cached file is too old, create new
  73. $result = $this->Parse($rss_url);
  74. $serialized = serialize($result);
  75. if ($f = @fopen($cache_file, 'w')) {
  76. fwrite ($f, $serialized, strlen($serialized));
  77. fclose($f);
  78. }
  79. if ($result) $result['cached'] = 0;
  80. }
  81. }
  82. // If CACHE DISABLED >> load and parse the file directly
  83. else {
  84. $result = $this->Parse($rss_url);
  85. if ($result) $result['cached'] = 0;
  86. }
  87. // return result
  88. return $result;
  89. }
  90.  
  91. // -------------------------------------------------------------------
  92. // Modification of preg_match(); return trimed field with index 1
  93. // from 'classic' preg_match() array output
  94. // -------------------------------------------------------------------
  95. function my_preg_match ($pattern, $subject) {
  96. // start regullar expression
  97. preg_match($pattern, $subject, $out);
  98.  
  99. // if there is some result... process it and return it
  100. if(isset($out[1])) {
  101. // Process CDATA (if present)
  102. if ($this->CDATA == 'content') { // Get CDATA content (without CDATA tag)
  103. $out[1] = strtr($out[1], array('<![CDATA['=>'', ']]>'=>''));
  104. } elseif ($this->CDATA == 'strip') { // Strip CDATA
  105. $out[1] = strtr($out[1], array('<![CDATA['=>'', ']]>'=>''));
  106. }
  107.  
  108. // If code page is set convert character encoding to required
  109. if ($this->cp != '')
  110. //$out[1] = $this->MyConvertEncoding($this->rsscp, $this->cp, $out[1]);
  111. $out[1] = iconv($this->rsscp, $this->cp.'//TRANSLIT', $out[1]);
  112. // Return result
  113. return trim($out[1]);
  114. } else {
  115. // if there is NO result, return empty string
  116. return '';
  117. }
  118. }
  119.  
  120. // -------------------------------------------------------------------
  121. // Replace HTML entities &something; by real characters
  122. // -------------------------------------------------------------------
  123. function unhtmlentities ($string) {
  124. // Get HTML entities table
  125. $trans_tbl = get_html_translation_table (HTML_ENTITIES, ENT_QUOTES);
  126. // Flip keys<==>values
  127. $trans_tbl = array_flip ($trans_tbl);
  128. // Add support for &apos; entity (missing in HTML_ENTITIES)
  129. $trans_tbl += array('&apos;' => "'");
  130. // Replace entities by values
  131. return strtr ($string, $trans_tbl);
  132. }
  133.  
  134. // -------------------------------------------------------------------
  135. // Parse() is private method used by Get() to load and parse RSS file.
  136. // Don't use Parse() in your scripts - use Get($rss_file) instead.
  137. // -------------------------------------------------------------------
  138. function Parse ($rss_url) {
  139. // Open and load RSS file
  140. if ($f = @fopen($rss_url, 'r')) {
  141. $rss_content = '';
  142. while (!feof($f)) {
  143. $rss_content .= fgets($f, 4096);
  144. }
  145. fclose($f);
  146.  
  147. // Parse document encoding
  148. $result['encoding'] = $this->my_preg_match("'encoding=['"](.*?)['"]'si", $rss_content);
  149. // if document codepage is specified, use it
  150. if ($result['encoding'] != '')
  151. { $this->rsscp = $result['encoding']; } // This is used in my_preg_match()
  152. // otherwise use the default codepage
  153. else
  154. { $this->rsscp = $this->default_cp; } // This is used in my_preg_match()
  155.  
  156. // Parse CHANNEL info
  157. preg_match("'<channel.*?>(.*?)</channel>'si", $rss_content, $out_channel);
  158. foreach($this->channeltags as $channeltag)
  159. {
  160. $temp = $this->my_preg_match("'<$channeltag.*?>(.*?)</$channeltag>'si", $out_channel[1]);
  161. if ($temp != '') $result[$channeltag] = $temp; // Set only if not empty
  162. }
  163. // If date_format is specified and lastBuildDate is valid
  164. if ($this->date_format != '' && ($timestamp = strtotime($result['lastBuildDate'])) !==-1) {
  165. // convert lastBuildDate to specified date format
  166. $result['lastBuildDate'] = date($this->date_format, $timestamp);
  167. }
  168.  
  169. // Parse TEXTINPUT info
  170. preg_match("'<textinput(|[^>]*[^/])>(.*?)</textinput>'si", $rss_content, $out_textinfo);
  171. // This a little strange regexp means:
  172. // Look for tag <textinput> with or without any attributes, but skip truncated version <textinput /> (it's not beggining tag)
  173. if (isset($out_textinfo[2])) {
  174. foreach($this->textinputtags as $textinputtag) {
  175. $temp = $this->my_preg_match("'<$textinputtag.*?>(.*?)</$textinputtag>'si", $out_textinfo[2]);
  176. if ($temp != '') $result['textinput_'.$textinputtag] = $temp; // Set only if not empty
  177. }
  178. }
  179. // Parse IMAGE info
  180. preg_match("'<image.*?>(.*?)</image>'si", $rss_content, $out_imageinfo);
  181. if (isset($out_imageinfo[1])) {
  182. foreach($this->imagetags as $imagetag) {
  183. $temp = $this->my_preg_match("'<$imagetag.*?>(.*?)</$imagetag>'si", $out_imageinfo[1]);
  184. if ($temp != '') $result['image_'.$imagetag] = $temp; // Set only if not empty
  185. }
  186. }
  187. // Parse ITEMS
  188. preg_match_all("'<item(| .*?)>(.*?)</item>'si", $rss_content, $items);
  189. $rss_items = $items[2];
  190. $i = 0;
  191. $result['items'] = array(); // create array even if there are no items
  192. foreach($rss_items as $rss_item) {
  193. // If number of items is lower then limit: Parse one item
  194. if ($i < $this->items_limit || $this->items_limit == 0) {
  195. foreach($this->itemtags as $itemtag) {
  196. $temp = $this->my_preg_match("'<$itemtag.*?>(.*?)</$itemtag>'si", $rss_item);
  197. if ($temp != '') $result['items'][$i][$itemtag] = $temp; // Set only if not empty
  198. }
  199. // Strip HTML tags and other bullshit from DESCRIPTION
  200. if ($this->stripHTML && $result['items'][$i]['description'])
  201. $result['items'][$i]['description'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['description'])));
  202. // Strip HTML tags and other bullshit from TITLE
  203. if ($this->stripHTML && $result['items'][$i]['title'])
  204. $result['items'][$i]['title'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['title'])));
  205. // If date_format is specified and pubDate is valid
  206. if ($this->date_format != '' && ($timestamp = strtotime($result['items'][$i]['pubDate'])) !==-1) {
  207. // convert pubDate to specified date format
  208. $result['items'][$i]['pubDate'] = date($this->date_format, $timestamp);
  209. }
  210. // Item counter
  211. $i++;
  212. }
  213. }
  214.  
  215. $result['items_count'] = $i;
  216. return $result;
  217. }
  218. else // Error in opening return False
  219. {
  220. return False;
  221. }
  222. }
  223. }
  224.  
  225. echo(Get("http://www.motoflesz.pl/rss.php")); 
  226. ?>
  227.  
  228.  
  229.  
  230. </head>
  231.  
  232. <body>
  233.  
  234. </body>
  235. </html>
dr_bonzo
Temat: Zasady panujace na forum Algorytmy klasy funkcje
--> Gotowe skrypty
Nilhir
  1. <?php
  2. include "lastRSS.php";
  3.  
  4. $rss = new lastRSS; 
  5. $content = $rss->get('http://www.motoflesz.pl/rss.php');
  6. foreach ($content['items'] as $news)
  7. {
  8.  echo '> <a href="'.$news['link'].'">'.$news['title']</a><br />';
  9. }
  10. ?>

to jest tylko przyk³ad bo mo¿esz to modyfikowaæ i dorzuciæ cache
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.