Welcome to WebmasterWorld Guest from 54.167.144.170

Forum Moderators: coopster & jatar k

Twitter RSS on your site

Works but need to be better

   
7:53 pm on Feb 23, 2012 (gmt 0)

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I am playing with a few elements inherent to a class pertaining to PHP.
Here is what I try to achieve:
Display your own Twitter RSS on your site.
FYI information your Twitter' RSS is no longer visible but still working, so to grab yours just change the username in the feed's url.

The following code snippet works,(Don't pay attention to formatting)
but I cannot figure how to make non active each Tweet content,the title of each tweets contains the ID so the content does not need to be a link, but I cannot figure how to do that.
I also commented a link to the manual about the class.
To test it just change the username, load it up and and output it in your browser.
<?php
// [php.net...]

class CeiXML extends SimpleXMLElement{
public function asHTML(){
$ele=dom_import_simplexml($this);
$dom = new DOMDocument('1.0', 'utf-8');
$element=$dom->importNode($ele,true);
$dom->appendChild($element);
return $dom->saveHTML();
}
}
echo"<table width='200'>";
//Define feed URL
$feed_url = 'https://twitter.com/statuses/user_timeline/YOUR TWITTER USERNAME.rss';
//Get content of the URL
$content = file_get_contents($feed_url);
//Prase feeds into class
$feeds = new CeiXML($content);
echo '<tr><td><strong><a href="' .$feeds->channel->link.'</a><br>'. '" title="' .$feeds->channel->title. '">' .$feeds->channel->title. '
</strong></td></tr>';
echo '<tr><td><ul>';
//Output feeds
foreach($feeds->channel->item as $feed2) {
echo '<li><style type="text/css">
a { text-decoration:none }
</style><a href="' .$feed2->link.'</a><br>">' .$feed2->title. '</li>';
}
echo '</ul></td></tr>';

?>
1:06 pm on Feb 27, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't pay attention to formatting


IMHO, it might save you some trouble...

Check the HTML source of your script's output. The links are rather messy.
1:08 pm on Feb 27, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a hint:

Replace
<a href="' .$feeds->channel->link.'</a>
by
<a href="' .$feeds->channel->link.'">anchor text here</a>
1:58 pm on Feb 27, 2012 (gmt 0)

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Thanks, I am not sure I was clear enough,
your solution does not offer what I try to do,
as it it echoes fine all tweets
example;

tweet1
tweet body 1
tweet2
tweet body 2
etc...

Where:
tweet1 (contains link to it and tweet id)
and
tweet body 1 (the tweet body is also activated and I wish it to be non an activated link, just txt)

it does not look good to have a whole col activated blue
So how to separate the tweet's content from the top link to the tweet?

I don't see how it could be done
did you try to plug it and try it?
2:53 pm on Feb 27, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest using sprintf() [php.net] instead of concatenating the output; it makes it easier to code proper HTML, which I think is (at least part of) the issue here.

Also, it may be useful to remove the <style> element from the loop: there is no use in having the same rule declared more than once.

Tweet ID doesn't seem to be a property of $feed2, so you'll need to derive it from either $feed2->link or $feed2->guid (they're identical).
3:02 pm on Mar 2, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This should get you started:

printf('<tr><td><strong><a href="%s" title="%s">%s</a></strong></td></tr>', $feeds->channel->link, $feeds->channel->title, $feeds->channel->title);
echo '<tr><td><ul>';
foreach($feeds->channel->item as $item) {
printf('<li><a href="%s">%s</a><br>%s</li>' . PHP_EOL, $item->link, $item->pubDate, $item->title);
}
echo '</ul></td></tr>';


Note that this displays the timestamp instead of the ID - seemed more useful to me.
5:02 pm on Mar 2, 2012 (gmt 0)

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Thanks to RonPK for adding the crucial tweaks!

Note:don't know why the the boxed comments on this post
are not showing as a neat rectangle.

<?php
###################################################################
# This snippet utilises a PHP native class.########################
# ManyThanks to Webmasterworld member RonPK for his collaboration##
###################################################################

// $feed_url replace YOUR TWITTER USERNAME.rss
// by your own username

class CeiXML extends SimpleXMLElement{
public function asHTML(){
$ele=dom_import_simplexml($this);
$dom = new DOMDocument('1.0', 'utf-8');
$element=$dom->importNode($ele,true);
$dom->appendChild($element);
return $dom->saveHTML();
}
}
echo"<table width='200'>";
//Define feed URL
$feed_url = 'https://twitter.com/statuses/user_timeline/YOUR TWITTER USERNAME.rss';
//Get content of the URL
$content = file_get_contents($feed_url);
//Prase feeds into class

$feeds = new CeiXML($content);
printf('<tr><td><strong><a href="%s" title="%s">%s</a></strong></td></tr>', $feeds->channel->link, $feeds->channel->title, $feeds->channel->title);
echo '<tr><td><ul>';
foreach($feeds->channel->item as $item) {
printf('<li><a href="%s">%s</a><br>%s</li>' . PHP_EOL, $item->link, $item->pubDate, $item->title);
}
echo '</ul></td></tr></table>';

?>
5:19 pm on Mar 2, 2012 (gmt 0)

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Addendum:
To limit returned results, limit the foreach
example is set at 2
note we use "break"

$feeds = new CeiXML($content);
printf('<tr><td><strong><a href="%s" title="%s">%s</a></strong></td></tr>', $feeds->channel->link, $feeds->channel->title, $feeds->channel->title);
echo '<tr><td><ul>';
$i=0;
foreach($feeds->channel->item as $item) {
printf('<li><a href="%s">%s</a><br>%s</li>' . PHP_EOL, $item->link, $item->pubDate, $item->title);
if (++$i == 2) break;
}
echo '</ul></td></tr></table>';

?>
 

Featured Threads

Hot Threads This Week

Hot Threads This Month