see613

index.php

Aug 2nd, 2012
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.14 KB | None | 0 0
  1. <?php
  2.  
  3.     class BlogPost
  4.     {
  5.         var $date;
  6.         var $ts;
  7.         var $link;
  8.    
  9.         var $title;
  10.         var $text;
  11.     }
  12.    
  13.     class BlogFeed
  14.     {
  15.         var $posts = array();
  16.    
  17.         function BlogFeed($feed_uri)
  18.         {
  19.             $xml_source = file_get_contents($feed_uri);
  20.             $x = simplexml_load_string($xml_source);
  21.    
  22.             if(count($x) == 0)
  23.                 return;
  24.    
  25.             foreach($x->channel->item as $item)
  26.             {
  27.                 $post = new BlogPost();
  28.                 $post->date = (string) $item->pubDate;
  29.                 $post->ts = strtotime($item->pubDate);
  30.                 $post->link = (string) $item->link;
  31.                 $post->title = (string) $item->title;
  32.                 $post->text = (string) $item->description;
  33.    
  34.                 // Create summary as a shortened body and remove images, extraneous line breaks, etc.
  35.                 $summary = $post->text;
  36.                 $summary = preg_replace('/<img[^>]*>/i', "", $summary);
  37.                 $summary = preg_replace('/^(<br[ ]?\/>)*/i', "", $summary);
  38.                 $summary = preg_replace('/(<br[ ]?\/>)*$/i', "", $summary);
  39.    
  40.                 // Truncate summary line to 100 characters
  41.                 $max_len = 100;
  42.                 if(strlen($summary) > $max_len)
  43.                     $summary = substr($summary, 0, $max_len) . '...';
  44.    
  45.                 $post->summary = $summary;
  46.    
  47.                 $this->posts[] = $post;
  48.             }
  49.         }
  50.     }
  51.        
  52.    
  53.    $blogFeed = new BlogFeed('http://rss.cnn.com/rss/edition_world.rss');
  54.    $feed = $blogFeed->posts;
  55.    
  56.    $weHaveElements = false;  
  57.    $result = '{ "items": [';
  58.        
  59.    if (count($feed) > 0) {
  60.         $weHaveElements = true;
  61.        
  62.         foreach ($feed as $i => $value)
  63.         {
  64.             $result .= '{"id": "'.$i.'", "html": "<a href=\"'.$value->link.'\">'.str_replace('"', '\"', $value->title).'</a><br />'.str_replace('"', '\"', $value->summary).'<br />'.$value->date.'"}, ';
  65.         }
  66.    }
  67.  
  68.    $result = $weHaveElements ? substr($result, 0, -2) : $result;
  69.    echo $result.' ] }';
  70. ?>
Advertisement
Add Comment
Please, Sign In to add comment