The code below no longer works as-is with the Twitter API update to 1.1 as of 2013-06-11. The 1.1 API requires oAuth which requires a server side component. Here’s a PHP way to interact with the new API. Googling around will find you many other libraries and such. There is an all-JavaScript method for getting tweets, but it may be a bit of a loophole that you may not want to count on.
$.getJSON("https://api.twitter.com/1/statuses/user_timeline/chriscoyier.json?count=1&include_rts=1&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 replace the contents of.
You can play around with this on this CodePen (deprecated).
Love it!
But how can I show the last several tweets?
Add this
callback=twitterCallback2&count=1
after this
callback=?
Change the 1 to as many as you’d like.
I mean, replace the
callback=?
with the first code.hey its working but how to display user name’s name and picture…
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.
There is definitely a limit to the number of request you can make. So storing tweet information (in a database or otherwise) is pretty much a must.
For an unauthenticated query to twitter, there is a limit of 150 requests per hour.
You can read all about it in twitters dev. documentation here.
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..
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: `
`
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.
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…
works for me. thank you
This is not working in IE at the moment? Any ideas?
Use a modern browser like safari, firefox or chrome and ditch your prehistoric IE.
IE is like the fat, dumb kid at school that everyone had to wait behind for in every class and carry on their backs in all the sports.
Instructing a developer to ditch a browser is a non-answer. The developer has no control over which browser the user is going to attempt to view the page with. I have run into many cases where I have been instructed to develop toward browsers as old as IE7. Some organizations will not allow browser upgrades without long drawn-out testing processes that never happen. Likewise, some organizations will not allow users to install Chrome, or other non-Microsoft browsers for security reasons.
Works fine for me!!
don’t forget to include jQuery library! lol
RJ, use the code that Inigo posted. Just make sure to change all of the quotes as they get messed up formatting if pasted into certain text editors. It works fine in all current versions of IE, Chrome, Firefox, and Opera…
How do I exclude links from tweets returned? I do not want any tweets with links coming in. I know there is a filter:links paramater but not sure how to implement.
Any idea why this works with all my twitter usernames except the one below?
Doesn’t work after retweet.
“This method will not include retweets in the XML and JSON responses unless the include_rts parameter is set. The RSS and Atom responses will always include retweets as statuses prefixed with RT, regardless of provided parameters.”
https://dev.twitter.com/docs/api/1/get/statuses/user_timeline
How do I make random tweets?
To display last five tweets using jQuery:
$.getJSON(“http://twitter.com/statuses/user_timeline/NoReservations.json?callback=?&count=6”, function(data) {
/* $(“#twitter”).html(data[0].text);*/
$.each(data, function(i, object) {
$(“#twitter”).append(”+object.text+”);
});
});
this works for me, for several tweets (5 in this case) with links.
http://jsfiddle.net/8jmLY/664/
if I’m not late for this party, this seems to work for me though. using jquery framework, put this in you html/php page and create as and inside it put this code. change the username to yours.
So, followed this (and the latest comments), and no problem pulling the latest tweet at all. Easy as pie, actually. Terrific.
Looking through the json data pulled from Twitter, I really want to pull the “statuses_count” – i.e., the number of tweets you’ve posted. It’s displayed several times here: https://twitter.com/statuses/user_timeline/kyle_conrad.json?callback=? as well as here: https://api.twitter.com/1/users/show.json?screen_name=kyle_conrad&include_entities=true (and outlined here: https://dev.twitter.com/docs/api ).
However, I can’t seem to pull that – pulling text is no problem, but any of the other JSON data that I want to pull just returns an “undefined.” There has to be something simple that I’m doing wrong, right?
Hello everyone,
I was wondering, how do I display the lastest tweet in one div. Then the 2nd tweet in another div and then the 3rd tweet in another div. Something along the lines of this:
(div)lastest tweet(/div)
(div)random picture(/div)
(div)random picture(/div)
(div)2nd tweet(/div)
(div)random article(/div)
(div)random picture(/div)
(div)third tweet(/div)
how would i go about that? I’m still looking but I dont even know the terminology to even search. Aggregate?
Thanks.
hi was just wondering what i need to input to have the time of tweet showed e.g. 17 Mins ago , 1 hr ago etc
Here is an article that shows you how to use javascript to do what you want. Basically, in the returned JSON information there is a variable called “created__at.”
You basically have to write your own math to figure out from the created_at timestamp into a “1 hour ago” sort of information.
Also you might want to delve into Twitters API documentation: https://dev.twitter.com
This no longer works, there seems to be some update by twitter recently …
It used to work fine but now I get this error : Sorry, that page does not exist”,”code”:34
Any workaround or quickfixes to this??
I’m wondering the same thing..
I’m agree, #Oumz, this doesn’t work since yesterday!
I also agree with #Oumz. I can’t get any scripts to work.
Thanks for the interesting post, I was using the old simple two lines of code below like many other people but
now it seems Twitter has prohibited this and we are all left with empty areas. Is there any way around this or do we really have to put up with the ugly widgets??
Sorry, it didn’t post.
src=”http://twitter.com/javascripts/blogger.js”>
src=”http://twitter.com/statuses/user_timeline/username.json?callback=twitterCallback2&count=2″>
Works like a charm! Thanks!
Worked like a charm!!!! :) Thank you.
how can I use it on html?
Hi does anyone have a fix so the tweets will display with clickable links? I’ve been trying to get the following to work with no luck
function twitterCallback2(twitters) {
var statusHTML = [];
for (var i=0; i<twitters.length; i++){
var username = twitters[i].user.screen_name;
var status = twitters[i].text.replace(/((https?|s?ftp|ssh):\/\/[^”\s\<>]*[^.,;'”>:\s\<>)]!])/g, function(url) {
return ‘‘+url+’‘;
}).replace(/\[email protected]([_a-z0-9]+)/ig, function(reply) {
return reply.charAt(0)+’‘+reply.substring(1)+’‘;
});
statusHTML.push(‘
“’+status+’” – ‘+relative_time(twitters[i].created_at)+’
‘);
}
$(‘.loading’).fadeOut(750, function() {
$(‘#latest_tweet’).append($(statusHTML.join(”)).hide().fadeIn(750));
});
}
Hiya, I’m just wondering if there’s a way to add the time to this, as well as the username. e.g., “12:22 by @username”
Thanks !
I’m trying to add this to my site, but it doesn’t seem to be working, I’m relatively new to web development and I’m sure its a simple oversight on my part but how should I connect the js file to my html file?
It’s not as easy to implement as Chris’s code up above but I found the MiniTwitter jQuery plugin that allows for the ability to be style in css. Very clean & so far is reliable.
Can I specify a hashtag so that it brings back the latest tweet with a particular hashtag in?
Thanks for this!
Anyway how can I get the date of the tweet when it has been posted?
Best,
John
Just wondering if this means I’ll have to use the widget eventually instead of this? Because I really like Chris’ solution! A widget will be nowhere near as elegant I assure you
Regards,
Scott
Exactly what I was looking for – simple and elegant code. Cheers.
Since this is still one of the top results I feel I should mention this method is going to completely stop working in the near future. This is using version 1 of Twitter’s API, which is now deprecated and will be removed at some point (possibly next month according to one post I read). The new version (1.1) requires all calls to be authenticated using OAuth, which means the days of easily grabbing tweets like this are gone.
Dang Twitter, you definitely deserve the angry old man fist shake over there.
Also, Thanks Chris and everyone who contributes to this site, it’s a developer’s best friend!
@Aron Duby
Unfortunately you’re completely correct. I went to knock up a quick splash page for a client today using this code, as i’ve done since Mr Coyier originally posted it, and i found it no longer works.
Time to hunt down an adequate solution as nothing Twitter provides is half as good for use.
A lot of people have ben asking how to convert the text urls to actual urls
the website is not displaying teh full code .. so here’s a link to it
http://pastie.org/6503759
I’m planning on using this on my blog soon. Thanks! :)
No, it doesn’t displaying my tweets. Please help. I think Twitter updated the API.
Please update the post.
Using a few of the comments previously posted here, I have created a handy snippet for anyone to use. All that’s needed is the twitter handle. It can be easily amended to display multiple previous tweets, and not just the most recent.
Enjoy!
11/06/2013 – 10pm – RIP API 1.0 It seems Twitter API has moved to version 1.1 and the authentication is now requiered for all kind of requests… We can’t even get a tweet! We’ll now have to get it through our server (no more JSONP).
ouch it’s true, this whole page is useless now. anyone got a method to fetch the latest tweet that works?
how can i display the fotmat-less last tweet with this new api version?
http://stackoverflow.com/questions/12916539/simplest-php-example-retrieving-user-timeline-with-twitter-api-version-1-1
This doesn’t seems to work now, please update this article. Thank you.
what r d things should i install before fetching the tweets? what r the softwares i required for this i dont know.please help me for this. i want the code for search the twitters ……
Hi,
Not working anymore.
This is when technology becomes a pain in the ***.
I really hate having to go back and change things like these just because Twitter decided to make changes.
I want to display the post time like about 19 hours ago.how do i do?
Hi can you update this post for twitter api changes.
https://dev.twitter.com/docs/api/1.1/overview
FYI This is out of date now
Thanks Cody
Is there a way to display the tweet count for a particular post like the way jetpack plugin does?
We can use Ruby to get the latest tweet:
Error message :
The Twitter REST API v1 is no longer active. Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.
according to the documentation of twitter api,
API v1.1 is released, user authorization (and access tokens) are required for all API 1.1 requests. In the weeks following release, some methods will require only application-based authentication for certain “userless” contexts.
thanks you for your code, you helped me!
I like This Example Which is well work for me
http://codepen.io/jasonmayes/pen/Ioype
Hi
i got this error message.
“This no longer works as of Twitter API 1.1”
Can u plz help me how i can remove this