Advertisement
Guest User

PHP JSONP

a guest
Apr 19th, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.46 KB | None | 0 0
  1. <?php  
  2.     /**
  3.     *
  4.     *How does it work ?
  5.     *   This class retrieves the news from the rss url. The array_n
  6.     *
  7.     *How To :
  8.     *
  9.     *   1st step :  $your_var =  new News ('rss.your_url.com');
  10.     *
  11.     *   2nd step :  call getNews function with the number of news you want to get
  12.     *       tip¹ : to print the news without controlling the array just uncomment and style as you wish  on RSSParser.class.php -> endElement function
  13.     *
  14.     *   3rd step :  with a foreach/for/while you can fetch the array : $array_news of this class to get your news.
  15.     *      
  16.     *               example:       
  17.     *                           <?
  18.     *                       $folha_news = new News("http://feeds.folha.uol.com.br/emcimadahora/rss091.xml");
  19.     *                       $folha_news->getNews();
  20.     *                       $numb_news=1;
  21.     *                       foreach ($folha_news->array_news as $key => $new) {?>
  22.     *                           <div class="noticia_rss">
  23.     *                               <div><span><?=$new['title']?></span><span><a href="<?=$new['link']?>" target="__blank"> Leia mais..</a></span></div>
  24.     *                               <div class='description'><?=$new['description']?></div>
  25.     *                           </div>
  26.     *
  27.     *
  28.     *                       <?
  29.     *                           $numb_news++;
  30.     *                           if ($numb_news >4) {
  31.     *                               break;
  32.     *                           }
  33.     *                       }
  34.     *                       //var_dump($folha_news->array_news);
  35.     *                   ?>
  36.     *       tip¹ : 'description' may be null in some rss news.
  37.     *
  38.     */
  39.  
  40.     class News
  41.     {
  42.  
  43.         var $rss_url;
  44.         var $rss_parser;
  45.         var $array_news;
  46.  
  47.         function __construct($url)
  48.         {
  49.             $this->rss_url = $url;
  50.             $this->getNews();
  51.         }
  52.  
  53.         public function getNews()
  54.         {
  55.             $this->array_news = array();
  56.             $feed_url = $this->rss_url;
  57.             $xml_parser = xml_parser_create();
  58.             $this->rss_parser = new RSSParser();
  59.             xml_set_object($xml_parser,$this->rss_parser);
  60.             xml_set_element_handler($xml_parser, "startElement", "endElement");
  61.             xml_set_character_data_handler($xml_parser, "characterData");
  62.             $fp = fopen("$feed_url","r")
  63.                or die("Error reading RSS data.");
  64.  
  65.             while ($data = fread($fp, 4096)){ // header+1 new =  approx. 850 bytes, 1 new = 230 bytes
  66.                 //echo "linha = ".xml_get_current_line_number($xml_parser)." <br>";
  67.                 //if (xml_get_current_line_number($xml_parser) < 2) {              
  68.                     xml_parse($xml_parser, $data, feof($fp))
  69.                     or die(sprintf("XML error: %s at line %d",  
  70.                         xml_error_string(xml_get_error_code($xml_parser)),  
  71.                         xml_get_current_line_number($xml_parser)));
  72.                 //}
  73.             }
  74.             fclose($fp);
  75.             xml_parser_free($xml_parser);
  76.             $this->array_news = $this->rss_parser->getNewsArray();
  77.         }
  78.  
  79.     }
  80. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement