Recently I’ve re-designed my home page veerasundar.com. I thought that adding my recent twitter status would spice up my home page little bit. I’ve seen this kind of status messages in many websites with the help of plug-ins. But I wanted use my JQuery knowledge. So, i started with the coding from scratch. My plan was really simple. I knew that JQuery has a AJAX function $.get(), which will do a HTTP GET request to a remote URL. I decided to use this function to make a call to Twitter API which will send me my recent tweet. But, the plan was obviously failed.
Access to restricted URI denied” code: 1012
Twitter has a API method statuses/user_timeline, with a parameter ‘count’. If we pass 1 to this ‘count’, the Twitter will return the recent status. For example, to get a user abcd’s recent tweet, the GET url will look like:
http://twitter.com/statuses/user_timeline/abcd.xml?count=1
But when I made a get request to this URL, Firefox returned me an error saying “Access to restricted URI denied” code: 1012″, which means that due to security reasons, it’s not possible to make a JS call to other domains. So, to resolve this problem, one solution would be making a call to a PHP program located in the same server, which will make a call to Twitter API and then will send us the twitter status. Thus, making a cross-domain JS call would be avoided.
PHP program to retrieve last twitter status:
Then I started with the PHP code which will act as a twitter status retriever for my page. Below is the code for my ‘twitter.php’.
<?php $twit = file_get_contents("http://twitter.com/statuses/user_timeline/abcd.xml?count=1"); echo $twit; ?>
So simple, isn’t it!? Next challenge is to get the XML content returned by Twitter server and to parse it to get the actual status message.
Parsing Twitter XML using JQuery
JQuery comes to rescue here. JQuery is a great library with in-built functions for parsing XML content. Below is the JQuery code to parse the Twitter XML response.
$.ajax({
type: "GET",
url: "twitter.php",
success: function(msg){
$status ="";
$tweets = "";
$followers = "";
$(msg).find("text").each(function(){
$status = "From Twitter: "+$(this).html();
});
$(msg).find("statuses_count").each(function(){
$tweets = $(this).html();
});
$(msg).find("followers_count").each(function(){
$followers = $(this).html();
});
}
});
So, that’s it. Now we have the twitter details in JavaScript variables $status, $tweets and $followers. Next we just need to replace this data in the web page.
Please let me know if this information is helpful to you. Please say in comments, if there is any better way to do this. And, thanks to Vinay Raikar for helping me in the initial stage.
{ 2 trackbacks }
{ 18 comments }
Use the JSON method – that’s much simpler. It lets you specify a Javascript function callback.
yes. Twitter provides the results in JSON format too. But, I think both JSON and XML formats need same amount of parsing.
JSON parsing is a lot easier in PHP and more efficient.
I’m not sure about PHP! but in my code, I’m using PHP just to get the data from Twitter and pass to JQuery. So, here the processing is done by JQuery.
In the next version, I’ll add JSON parsing also.
Binny,
Can you help w/ letting me know what the JSON method is? I’m trying to put my latest tweet on my website and having some trouble.
Hi John,
You can use the twitter API URL http://twitter.com/statuses/user_timeline/abcd.json?count=1 to get you last tweet in JSON format. Don’t forget to change ‘abcd’ in the above URL to the user name for which you want to get the tweet.
You can refer to this article http://blog.reindel.com/2007/10/02/parse-json-with-jquery-and-javascript/ for parsing the JSON data using JQuery/JavaScript.
Also I would have used CURL, since a lot of shared host turn off remote file loading like that.
Hi EllisGL,
Welcome to my blog!
Thanks for the information about CURL. Since I’m new two PHP (didn’t have much coding experience), I’d like to explore more such areas.
There’s lots to learn. I’ve been developing with PHP for 10 years now and there’s lot of stuff I don’t know. Lots of “hidden” things about PHP.
JSON parsing is a lot easier in PHP and more efficient.
You should also think about people without Javascript enabled.
This should be done in PHP using CURL. Simple, powerfull.
Agreed. If JavaScript is disabled, the above example (and none of the web 2.0 sites) won't work. In that case, as you said, an server side technique could solve the issue.
The most problem with Web 2.0 sites is that they are not usable. Really. In my development, I do everything server side first, then if everything is not working without javascript I do frontend development (animations, effects, etc.) Cheers!
//The most problem with Web 2.0 sites is that they are not usable//
may be web 2.0 sites are not 'accessible', but they are more usable than the usual websites, I think. Web 2.0 sites improve the normal user's experience with the website. But when it comes to the accessible features, such as screen readers, web 2.0 fails miserably.
btw, as you said, it is a good practice to finish up the server side business logic first and then go for the client side animation, effects, etc.
Veera, they are not usable too, for user without Javascript, don't you think so? Yes, they are fantastic improvment, but when they are coded correct way. My dad has Javascript turned off on his PC, do you imagine how many sites does not work? Without any information about it? Many. Really. And this is pain in the ass.
I got metal portal, many AJAX stuff used, but do you know how I made this? When you can add musician to the band (there's band database) there's URL href=”add_musician.php” for people without Javascript. In Javascript part of the website I load this file and added action in background.
btw, nice blog, like it:) btw2, are you from India? Your name sounds Hindi for me.
Cheers!
agreed with your view. With JS switched off, all web 2.0 sites definitely affects the user's experience.
thanks for the feed back. I'm glad that you liked my blog.
yeah, you are correct, I'm from southern part of India (Tamilnadu).
Tamil Nadu, called Country of Tamils in Tamil, right?
Great part of India
Will ready your blog and will add RSS feeds, nice to read! CheerS!
yes. you are correct, it's the country of Tamils.
Thanks Andrzej for your wishes!
Comments on this entry are closed.