Please login or register.

Login with username, password and session length
Advanced search  

News:

You need/want an older version of sNews ? Download an older/unsupported version here.

Author Topic: addon: show your latest twits  (Read 1582 times)

problemsolver

  • Newbie
  • *
  • Karma: 1
  • Posts: 2
    • Liman Yazı Atölyesi
addon: show your latest twits
« on: August 29, 2010, 08:34:17 AM »

Twitter is becoming more and more popular each day and we all use it to instantly notify our friends what we are doing just at that time. my addon will enable you to show your latest twits wherever you want by parsing the rss feed of your latest twits.

First, copy and paste this function in the snews.php:
Code: [Select]
function twits($uid, $count) {
$rss = file_get_contents('http://www.twitter.com/statuses/user_timeline/'.$uid.'.rss?count='.$count);
$xml = new simpleXMLElement($rss);
$items = $xml->channel->item;
foreach($items as $item) {
$parts = explode(':',$item->description);
echo "<p>".date('d.m.Y G:i', strtotime($item->pubDate))." - ".$parts[1]."</p>";
}
}
then in your index.php, call function twits() as in the following example:
Code: [Select]
twits(184015122,10);
well, the first argument is the user id in your rss feed link. for example, if your rss link is http://www.twitter.com/stasuses/user_timeline/184015122.rss, then your user id is 184015122. and the second argument is the number of the twits you want to show. simply replace the arguments with yours and you're done!
Logged

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Re: addon: show your latest twits
« Reply #1 on: August 29, 2010, 05:12:17 PM »

This part:
$parts = explode(':',$item->description);
Removes HTML links as well...

My (hackish) attempt is here:
Code: [Select]
<?php

function show_tweets($uid$count) {
$rss file_get_contents('http://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$uid.'&count='.$count);
$xml = new simpleXMLElement($rss);
$items $xml->channel->item;
foreach($items as $item) { 
// Date format
$date_format date('Y') != date('Y'strtotime($item->pubDate))
'g:i A M jS Y'
'g:i A M jS';
$date date($date_formatstrtotime($item->pubDate));

// Description format
$desc str_replace($uid.': '''$item->description);
// Link the @name within the tweet
$matches = array();
preg_match_all("(@[\w-]+)"$desc$matches);
if(!empty($matches[0])) {
for ($n=0$n<count($matches[0]); $n++) {
if (isset($matches[$n])) {
foreach( $matches[$n] as $match) {
$name substr($match1);
$desc str_replace(
$match
'<a href="http://twitter.com/'.$name.'">'.$match.'</a>'
$desc
);
}
}
}
}
unset($matches);

// Link the #searches 
$matches = array();
preg_match_all("(#[\w-]+)"$desc$matches);
if(!empty($matches[0])) {
for ($n=0$n<count($matches[0]); $n++) {
if (isset($matches[$n])) {
foreach( $matches[$n] as $match) {
$desc str_replace(
$match
'<a href="http://twitter.com/search?q='.$match.'">'.$match.'</a>'
$desc
);
}
}
}
}

echo '<p class="twitter_desc">'.$desc.'</p><p class="twitter_date">'.$date.'</p>';
}
}

show_tweets('incre'10);

?>

« Last Edit: August 29, 2010, 05:16:18 PM by nukpana »
Logged

problemsolver

  • Newbie
  • *
  • Karma: 1
  • Posts: 2
    • Liman Yazı Atölyesi
Re: addon: show your latest twits
« Reply #2 on: August 29, 2010, 08:22:57 PM »

many thanks for your reporting the bug first of all. :)

Surely yours is a complete solution but  below is another one for those who wants a simpler one:
Code: [Select]

function twits($uid, $count) {
$rss = file_get_contents('http://www.twitter.com/statuses/user_timeline/'.$uid.'.rss?count='.$count);
$xml = new simpleXMLElement($rss);
$items = $xml->channel->item;
foreach($items as $item) {
$parts = explode(':',$item->description);
$twit = str_replace($parts[0], "", $item->description);
echo "<p>".date('d.m.Y G:i', strtotime($item->pubDate))." - ".ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\" rel=\"nofollow\">\\0</a>", $twit) ."</p>";
}
}
twits('myusufsarigoz',10);
Logged

Sven

  • ULTIMATE member
  • ******
  • Karma: 88
  • Posts: 2029
  • Chasing MY bugs!
    • hiseo.fr - rédacteur Web
Re: addon: show your latest twits
« Reply #3 on: December 16, 2010, 06:30:59 PM »

Hum...
it seems that it doesn’t work with 1and1 server...  ???
(it woks locally).
file_get_contents drives to an issue:
Quote
String could not be parsed as XML
Any idea?

Kiss

  • Newbie
  • *
  • Karma: 0
  • Posts: 15
Re: addon: show your latest twits
« Reply #4 on: January 29, 2011, 10:19:06 AM »

i want screen shoot or demo please for this thing,

Would be great if here members can always give demo or at least screen shots whenever they post mods, addon.


Kindest Regards:)

Cheer's
Logged

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Re: addon: show your latest twits
« Reply #5 on: January 30, 2011, 06:22:43 PM »

Sven, to fix your issue on 1and 1 use this updated code:
Kiss, you can see an example of the outputted HTML at the end of the code
Code: [Select]
<?php

function show_tweets($uid$count) {

// If file_get_contents is diabled on your server...
$ch curl_init();
curl_setopt$chCURLOPT_URL'http://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$uid.'&count='.$count);
curl_setopt$chCURLOPT_RETURNTRANSFER);
$rss curl_exec$ch );
curl_close($ch);
// $rss = file_get_contents('http://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$uid.'&count='.$count);

$xml = new simpleXMLElement($rss);
$items $xml->channel->item;
foreach($items as $item) { 
// Replace username within the tweet
$item->description str_replace($uid.': '''$item->description);

// Format date like twitter
$date_format date('Y') != date('Y'strtotime($item->pubDate))
'g:i A M jS Y'
'g:i A M jS';
$item->pubDate date($date_formatstrtotime($item->pubDate));

// Link the @name within the tweet
$matches = array();
preg_match_all("(@[\w-]+)"$item->description$matches);
if(!empty($matches[0])) {
for ($n=0$n<count($matches[0]); $n++) {
if (isset($matches[$n])) {
foreach( $matches[$n] as $match) {
$name substr($match1);
$item->description str_replace(
$match
'<a href="http://twitter.com/'.$name.'">'.$match.'</a>'
$item->description
);
}
}
}
}
unset($matches);

// Link the #searches 
$matches = array();
preg_match_all("(#[\w-]+)"$item->description$matches);
if(!empty($matches[0])) {
for ($n=0$n<count($matches[0]); $n++) {
if (isset($matches[$n])) {
foreach( $matches[$n] as $match) {
$item->description str_replace(
$match
'<a href="http://twitter.com/search?q='.$match.'">'.$match.'</a>'
$item->description
);
}
}
}
}
}
return $items;
}


foreach( 
show_tweets('your-twitter-user-name'10) as $tweet ) {

echo '
<p class="twitter_desc">'
.$tweet->description.'</p>
<p class="twitter_date"><a href="'
.$tweet->link.'">'.$tweet->pubDate.'</a></p>
'
;


}

/* Example feed from FredK
<p class="twitter_desc">Suarez to LFC! Now there's a piece of news with some bite to it. :D</p>
<p class="twitter_date"><a href="http://twitter.com/incre/statuses/31069276236414976">2:21 PM Jan 28th</a></p>

*/


?>

« Last Edit: January 31, 2011, 02:46:28 AM by nukpana »
Logged

Fred K

  • Still trying to learn stuff
  • ULTIMATE member
  • ******
  • Karma: 130
  • Posts: 2728
    • Personal
Re: addon: show your latest twits
« Reply #6 on: January 30, 2011, 07:14:57 PM »

Quote from: nukpana
foreach( show_tweets('incre', 10) as $tweet ) {
   
   echo '
   <p class="twitter_desc">'.$tweet->description.'</p>
   <p class="twitter_date"><a href="'.$tweet->link.'">'.$tweet->pubDate.'</a></p>
   ';
   
   
}

The username in the quoted code is my username, not for everyone's use. Might be good to change that in the code (and output example)? Just so there's no confusion there. Thanks.
Logged

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Re: addon: show your latest twits
« Reply #7 on: January 31, 2011, 02:46:51 AM »

haha changed
Logged

Sven

  • ULTIMATE member
  • ******
  • Karma: 88
  • Posts: 2029
  • Chasing MY bugs!
    • hiseo.fr - rédacteur Web
Re: addon: show your latest twits
« Reply #8 on: January 31, 2011, 07:58:42 AM »

Now I can display Fred’s timeline. Great! ;)
CURL fixed it with your help. Thanks a lot Jason. 8)
Karma+.