Pomoc - Szukaj - U¿ytkownicy - Kalendarz
Pe³na wersja: [php] b³±d
Forum PHP.pl > Forum > PHP
macza
  1. <?php
  2. /*
  3.  +--------------------------------------------------+
  4.  | Author: Allan Irvine <ai@ayrsoft.com>  |
  5.  +--------------------------------------------------+
  6.  | Date: WED 03 MARCH 2004 |
  7.  +--------------------------------------------------+
  8.  | Origin: Scotland, United kingdom  |
  9.  +--------------------------------------------------+
  10.  | Script: newsReader.php  |
  11.  +--------------------------------------------------+
  12.  | No License  |
  13.  | free to use for personal and commercial purposes |
  14.  +--------------------------------------------------+
  15. */
  16.  
  17. //+++++++++++++++++++++++++++++++++++++++++++++++++++
  18. // CHANGES
  19. //---------------------------------------------------
  20. // Added a property $ampReplace, this is a quick fix
  21. // it is used to replace ampersands bfore php get a hold of
  22. // the rss file, and replace it once php has dealt with it
  23. // I was getting lines split by an ampersand, pointed out by
  24. // Mathew Clark of Divergent Systems, cheers.
  25. // I will need to address this later on as I deem this fix
  26. // as a bit of a kludge, not nice.
  27. // Allan Irvine <airvine@ayrsoft.com>
  28. // 29th April 2004
  29. //----------------------------------------------------
  30. //
  31. //++++++++++++++++++++++++++++++++++++++++++++++++++++
  32.  
  33.  
  34.  
  35. class newsReader{
  36.  
  37. var $content =  array();
  38. var $openTag =  array();
  39. var $closeTag =  array();
  40. var $currentTag;
  41. var $rss;
  42. var $level;
  43. var $startCollecting;
  44. var $wantedElements;
  45. var $cacheData;
  46. var $ampReplace;
  47.  
  48. function newsReader(){
  49. $this->rss = xml_parser_create();
  50. xml_set_object($this->rss, $this);
  51. xml_parser_set_option($this->rss,XML_OPTION_CASE_FOLDING,1);
  52. xml_set_element_handler($this->rss,_startElement,_endElement);
  53. xml_set_character_data_handler($this->rss,_parseData);
  54. $this->wantedElements['CHANNEL']  = array("TITLE","DESCRIPTION","LINK","IMAGES");
  55. $this->wantedElements['IMAGE']  = array("URL");
  56. $this->wantedElements['ITEM'] = array("TITLE","DESCRIPTION","LINK");
  57. }
  58.  
  59. function _findLevel($n){
  60. if(eregi("(CHANNEL)|(IMAGE)|(ITEM)",$n,$regs)){
  61. $this->level = $regs[0];
  62. }
  63. }
  64.  
  65. function _startToCollect($n){
  66. $this->startCollecting = (in_array($n,$this->wantedElements[$this->level]))?1:0;
  67. }
  68.  
  69. function _startElement($t,$name,$attribs){
  70. $this->currentTag = $name;
  71. $this->_findLevel($name);
  72. $this->_startToCollect($name);
  73. if($this->startCollecting){
  74. $this->openTag[]="<tr><td>n";
  75. }
  76. }
  77.  
  78. function _endElement($t,$name){
  79. if($this->startCollecting)
  80. $this->closeTag[]="</td></tr>n";
  81. }
  82.  
  83. function _parseData($t,$cData){
  84. $cData = str_replace($this->ampReplace,"&",$cData);// quick fix, will need to address later on
  85. if(strlen(trim($cData))&&($this->startCollecting)){
  86. if(eregi("link",$this->currentTag)){
  87. $this->content[]="<A HREF="".$cData."" TARGET="_BLANK">".$cData."</A>n";
  88. }elseif(eregi("url",$this->currentTag)){
  89. $this->content[]="<IMG SRC="".$cData."">n";
  90. }else{
  91. $this->content[]=$cData;
  92. }
  93. }
  94. }
  95.  
  96. function readNewsFeed($source){
  97.  
  98. $this->_readCacheData();
  99. if($this->_useCache($source)){
  100. $this->_writeCacheEntry($source);
  101. $file = file("newsCache/".md5($source));
  102. }else{
  103. $file = file($source);
  104. }
  105. $file = implode("n",$file);
  106. $this->ampReplace = md5(time());// quick fix, will need to address later on
  107. $file = str_replace("&",$this->ampReplace,$file);// quick fix, will need to address later on
  108.  
  109. if(!xml_parse($this->rss,$file,1)){
  110. $ln = xml_get_current_line_number($this->rss);
  111. $msg = xml_error_string(xml_get_error_code($this->rss));
  112. return ("An XML error occurred on line $ln: $msg");
  113. }else{
  114. $rtn = "<TABLE border=0>n";
  115. for($i=0;$i<count($this->openTag);$i++){
  116. $rtn.= $this->openTag[$i].$this->content[$i].$this->closeTag[$i];
  117. }
  118. $rtn.="</TABLE>n";
  119.  
  120. if(!$this->_useCache($source)){
  121. $this->_writeCacheEntry($source);
  122. }
  123.  
  124. return $rtn;
  125.  
  126. }
  127. }
  128.  
  129.  
  130. function _checkCacheDir(){
  131. if(!is_dir("newsCache")){
  132. mkdir("newsCache");
  133. }
  134. }
  135.  
  136. function _readCacheData(){
  137. $file = file("newsCache/newsReaderCache.php");
  138. if(strlen(trim($file[0]))){
  139. $this->cacheData = unserialize($file[0]);
  140. }
  141. }
  142.  
  143. function _useCache($source){
  144. if($key = array_search($source,$this->cacheData)){
  145. $sec = explode(" ",$key);
  146. if((time() - $sec[1])>(3600)){
  147. return 0;
  148. }else{
  149. return 1;
  150. }
  151. }else{
  152. return 0;
  153. }
  154. }
  155.  
  156. function _writeCacheEntry($source){
  157. $this->_checkCacheDir();
  158. $this->_updateCacheArray($source);
  159. $fp = fopen("newsCache/newsReaderCache.php","w");
  160. fwrite($fp,serialize($this->cacheData));
  161. fclose($fp);
  162.  
  163. $data = file($source);
  164. $data = implode("n",$data);
  165. $fp = fopen("newsCache/".md5($source),"w");
  166. fwrite($fp,$data);
  167. fclose($fp);
  168.  
  169. }
  170.  
  171. function _updateCacheArray($source){
  172.  
  173. if(is_array($this->cacheData) && in_array($source,$this->cacheData)){
  174. foreach($this->cacheData as $key=>$val){
  175. if(trim($val) == trim($source)){
  176. $key =microtime();
  177. }
  178. $new[$key]= $val;
  179. }
  180. $this->cacheData = $new;
  181. }else{
  182. $this->cacheData[microtime()] = $source;
  183. }
  184. }
  185.  
  186. }
  187.  
  188. //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  189. //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  190. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  191. //
  192. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  193. // test data below once you are happy how it works just remove this stuff
  194. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  195.  
  196. // bbc news stories
  197.  
  198. $newsSources['BBC']['world']="http://news.bbc.co.uk/rss/newsonline_uk_edition/world/rss091.xml";
  199. $newsSources['BBC']['uk']="http://news.bbc.co.uk/rss/newsonline_uk_edition/uk/rss091.xml";
  200. $newsSources['BBC']['england']="http://news.bbc.co.uk/rss/newsonline_uk_edition/england/rss091.xml";
  201. $newsSources['BBC']['norther ireland']="http://news.bbc.co.uk/rss/newsonline_uk_edition/northern_ireland/rss091.xml";
  202. $newsSources['BBC']['scotland']="http://news.bbc.co.uk/rss/newsonline_uk_edition/scotland/rss091.xml";
  203. $newsSources['BBC']['wales']="http://news.bbc.co.uk/rss/newsonline_uk_edition/wales/rss091.xml";
  204. $newsSources['BBC']['business']="http://news.bbc.co.uk/rss/newsonline_uk_edition/business/rss091.xml";
  205. $newsSources['BBC']['uk politics']="http://news.bbc.co.uk/rss/newsonline_uk_edition/uk_politics/rss091.xml";
  206. $newsSources['BBC']['health']="http://news.bbc.co.uk/rss/newsonline_uk_edition/health/rss091.xml";
  207. $newsSources['BBC']['education']="http://news.bbc.co.uk/rss/newsonline_uk_edition/education/rss091.xml";
  208. $newsSources['BBC']['science technology']="http://news.bbc.co.uk/rss/newsonline_uk_edition/sci/tech/rss091.xml";
  209. $newsSources['BBC']['technology']="http://news.bbc.co.uk/rss/newsonline_uk_edition/technology/rss091.xml";
  210. $newsSources['BBC']['entertainment']="http://news.bbc.co.uk/rss/newsonline_uk_edition/entertainment/rss091.xml";
  211. $newsSources['BBC']['talking point']="http://news.bbc.co.uk/rss/newsonline_uk_edition/talking_point/rss091.xml";
  212. $newsSources['BBC']['magazine']="http://news.bbc.co.uk/rss/newsonline_uk_edition/magazine/rss091.xml";
  213. $newsSources['BBC']['week at a glance']="http://news.bbc.co.uk/rss/newsonline_uk_edition/week_at-a-glance/rss091.xml";
  214. $newsSources['BBC']['programmes']="http://news.bbc.co.uk/rss/newsonline_uk_edition/programmes/rss091.xml";
  215.  
  216. // wired.com
  217.  
  218. $newsSources['Wired']['technology stories']="http://www.wired.com/news/feeds/rss2/0,2610,3,00.xml";
  219.  
  220. // yahoo
  221.  
  222. $newsSources['Yahoo']['Top Stories']="http://rss.news.yahoo.com/rss/topstories";
  223.  
  224.  
  225.  
  226.  
  227.  
  228. /*foreach($newsSources as $name=>$array){
  229.  
  230. echo $name;
  231. echo "<form method=post action=".$_SERVER['PHP_SELF'].">n";
  232. echo "<select name=source>n";
  233. foreach($array as $key=>$val){
  234. $sel = ($val == $source)? " SELECTED " : "";
  235. echo "<option $sel value=$val>$keyn";
  236. }
  237. echo "</select>n";
  238. echo "<input type=submit name=go value=Go>n";
  239. echo "</form>n";
  240. }*/
  241.  
  242. //++++++++++++++++
  243. // script usage |
  244. //++++++++++++++++
  245. #$source = "http://www.newscientist.com/syndication/news.rdf";
  246. #$go = 1;
  247. ?>
i teraz problem ...
wyskakuje taki blad:
Warning: in_array() [function.in-array]: Wrong datatype for second argument in C:\1\klient_1\newsReader.php on line 66
a 66 linia to:
  1. <?php
  2. function _startToCollect($n){
  3. $this->startCollecting = (in_array($n,$this->wantedElements[$this->level]))?1:0; //<<<<<<< ----66
  4. }
  5. ?>

jak to naprawic?
em1X
Mi zaden taki b³±d nie wyskakuje? :/

Nie wiem czy to cos da ale sprobuj zamienic:
  1. <?php
  2. function _findLevel($n){
  3. if(eregi("(CHANNEL)|(IMAGE)|(ITEM)",$n,$regs)){
  4. $this->level = $regs[0];
  5. }
  6. }
  7. ?>


na

  1. <?php
  2. function _findLevel($n) {
  3. $tab = array('CHANNEL', 'IMAGE', 'ITEM');
  4. for ($i=0; $i<3; $i++)
  5. if (count(explode($tab[$i], $n)) > 1)
  6. return $tab[$i];
  7.  
  8. return null;
  9. }
  10. ?>
dr_bonzo
Czekam na nowy tytul topiku na PW, wraz z linkiem do topiku
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.