Code Snippet
Display Last Tweet
$.getJSON("http://twitter.com/statuses/user_timeline/username.json?callback=?", function(data) {
$("#twitter").html(data[0].text);
});Make sure you change username.json to your actual username and #twitter to the actual selector you wish to update.
Love it!
But how can I show the last several tweets?
Add this
callback=twitterCallback2&count=1after this
callback=?Change the 1 to as many as you’d like.
I mean, replace the
callback=?with the first code.I tried to use the
It doesn’t seem to work for some reason…
Connecting to Twitter…
Yeah, similar sort of thing, can get the one on it’s own to work, but no more than that…
For those having an issue with more than one item, it’s because you have to put it inside of an array if it’s more than one item.
Something like:
instead of:
use
Will Twitter limit the number of jSON requests my server makes? In other words, if my site gets 1000 hits per hour should i be caching the tweet in a database? Or does twitter see it as the client making the request rather than my web server?
This site does OK without it… but… I probably should be caching. Caching something like this is just a good practice period. Regarding the limits, I’m not sure there is on stuff like just pulling from the timeline. I think it’s stuff like search that is limited. Could be wrong though.
you can also refer to this site on how to display your latest tweets in twitter. with twitter API.
http://www.ryscript.co.cc/web/how-to-display-latest-tweets-using-javascript-and-twitter-api/
thanks..
what if i dont want to get tweets from twitter i want to show it from my own mysql database any suggestions ?
Nice simple code..
I gonna use that one..
Thanks
It displays anchors as text on my page:
http://www.eclipse-webdesign.nl/contact
Any ideas?
works… thanks!
If you want to display your tweets with working links/hashtags/usernamelinks you could use this:
function parseTweet($text) {
$pattern_url = '~(?>[a-z+]{2,}://|www\.)(?:[a-z0-9]+(?:\.[a-z0-9]+)?@)?(?:(?:[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])(?:\.[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])+|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:/[^\\/:?*"|\n]*[a-z0-9])*/?(?:\?[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?(?:&[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?)*)?(?:#[a-z0-9_%.]+)?~i';
'@([A-Za-z0-9_]+)';
$tweet = preg_replace('/(^|\s)#(\w+)/', '\1#\2', $text);
$tweet = preg_replace('/(^|\s)@(\w+)/', '\1@\2', $tweet);
$tweet = preg_replace("#(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)#is", "\\1[link]", $tweet);
return $tweet;
}
This is awsome, I’ll try to implement this like now!!! :D
This did not work :(
@Daniel & others – Bob Kruithof’s code did not work for the following reasons:
He did not replace the html brackets with their ascii counterparts (as it tells you to do below), therefore the links in the code have been displayed as actual links in your browser (as you can see they are rendered in blue & you can click on them…) and
He hadn’t escaped some of the quotes in the last preg_replace.
The following code works perfectly. Just copy & paste it inside the div on your page where you want your tweet to be displayed, and change ‘YOURNAMEHERE’ to your name.
<?php
function parseTweet($text) {
$pattern_url = ‘~(?>[a-z+]{2,}://|www\.)(?:[a-z0-9]+(?:\.[a-z0-9]+)?@)?(?:(?:[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])(?:\.[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])+|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:/[^\\/:?*"|\n]*[a-z0-9])*/?(?:\?[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?(?:&[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?)*)?(?:#[a-z0-9_%.]+)?~i’;
‘@([A-Za-z0-9_]+)’;
$tweet = preg_replace(‘/(^|\s)#(\w+)/’, ‘\1#<a
href=”http://search.twitter.com/search?q=%23\2″ rel=”nofollow”>\2</a>’, $text);
$tweet = preg_replace(‘/(^|\s)@(\w+)/’, ‘\1@<a
href=”http://www.twitter.com/\2″ rel=”nofollow”>\2</a>’, $tweet);
$tweet = preg_replace(“#(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)#is”, “\\1<a
href=\”\\2\” title=\”\\2\” rel=\”nofollow\”>[link]</a>”, $tweet);
return $tweet;
}
$username=’YOURNAMEHERE’; // set user name
$format=’json’; // set format
$tweet=json_decode(file_get_contents(“http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}”)); // get tweets and decode them into a variable
$theTweet = parseTweet($tweet[0]->text);
echo ‘"’ . $theTweet . ‘"’;
?>
This above code does not work if copy and pasted. The pattern_url variable closes php ‘?>’ which I’m not sure is the intention – if it is than its never reopened which gives you the error. Not sure how to fix as I don’t know how to use patterns and preg functions. Anyone have any ideas?
had the same problem… just replace the all the ‘ and the ” — with ‘ and “…
works for me…
This is not working in IE at the moment? Any ideas?
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.