Showing posts with label RSS. Show all posts
Showing posts with label RSS. Show all posts

Dec 19, 2011

How To Show Latest Tweets Using PHP JQuery

Today I am sharing my newly PHP and JQuery coded snippet which is going to display your latest tweets in a fade in and fade out effect using Twitter timeline RSS feeds.



Here is the PHP function to get the tweets:

 
[php]
<?php
function latest_tweets(){
//global $post;
$doc = new DOMDocument();
$meta='webstutorial';
$feed = "http://twitter.com/statuses/user_timeline/$meta.rss";
$doc->load($feed);

$outer = '<ul id="tweets">';
$max_tweets = 15;
$i = 1;
foreach ($doc->getElementsByTagName('item') as $node) {
$tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;
//if you want to remove the userid before the tweets then uncomment the next line.
//$tweet = substr($tweet, stripos($tweet, ':') + 1);
$tweet = preg_replace('@(https?://([-w.]+)+(:d+)?(/([w/_.]*(?S+)?)?)?)@',
'<a href="$1">$1</a>', $tweet);
$tweet = preg_replace("/@([0-9a-zA-Z]+)/",
"<a href="http://twitter.com/$1">@$1</a>",
$tweet);

$outer .= "<li>". $tweet . "</li>n";


if($i++ >= $max_tweets) break;
}
$outer .= "</ul>n";
return "<div class='post'><p><b>Latest Tweets </b>".$outer."</div>";
}
echo latest_tweets();
?>
[/php]
 

In the above function we are fetching the RSS feed from the twitter timeline. Then we finally break it into segments and display it using the loop method.

Now we have to display the tweets with jquery effect.

So here is the jquery function to display the tweets in a fadeIn effect.

 
[js]
<script type="text/javascript">
function tweetRender( prospectID )
{
prospectID.delay() .fadeIn() .delay(2000).fadeOut(
function(){
if(prospectID.next().length > 0)
{tweetRender( prospectID.next() );}
else
{tweetRender( prospectID.siblings(':first'));}

}
);
}

$(function(){
$('#tweets li').hide();
tweetRender( $('#tweets li:first') );

});
</script>
[/js]
 

In this tutorial we are using basic PHP and JQuery to display the tweets. You can download it and check the Demo.

Nov 27, 2011

Youtube Video Scraping | Fetch Youtube Video Via RSS

Always I wondered to display youtube videos below my posts or for some other purpose. But always had to use some plugins or some complicated scripts. So finally coded for you people a small function which will fetch or scrape youtube videos through GDATA RSS.



[php]
<?php
$search = 'linkinpark';//Search Term
$file = file_get_contents('http://gdata.youtube.com/feeds/base/videos?q='.$search.'&client=ytapi-youtube-search&v=2');
$rss = new SimpleXMLElement($file);
$limit = '10'; //Videos Limit to display
$ctr = '0';
foreach($rss->entry as $idx => $key)
{
if($ctr == $limit)
{
break;
}
else{
echo '<div id="video">';
echo '<p class="title">'.$key->title.'</p>';
echo '<p class="video">'.$key->content.'</p>';
echo '</div>';
$ctr++;
}
}
?>
[/php]


My idea behind getting the videos is first to fetch the entire contents of the GDATA RSS page using file_get_contents

After storing the RSS into a file as a string, we will convert it into proper XML using SimpleXMLElement

We store this XML version into a variable which consists of arrays and objects.

Finally we bring the foreach and break down the array into pieces. Thats it. Go and test it yourself.

This function is the basic version, I am sure you can modify it and make more flexible.


download