In my previous post on this subject I spoke about making a simple call to the Twitter Search API to return some results every 30 seconds using jQuery and ajax. This time over I am going to discuss a similar process, however this time our API calls are authenticated with our Twitter credentials (to return your favorites in this example). So here goes…
PHP and cURL
Again I am going to use PHP with cURL to communicate with the Twitter API as well as return the XML. Here’s the code – create it in a file called getFavorites.php :
<?php
$username = "yourusername";
$password = "yourpassword";
$twitterHost = "http://twitter.com/favorites.xml";
$curl;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_URL, $twitterHost);
$result = curl_exec($curl);
curl_close($curl);
header('Content-Type: application/xml; charset=ISO-8859-1');
print $result;
?>
There’s alot here, the key parts being the first 3 variables, your username & password, and the API url. There are alot of cURL bits involved here, I won’t go into them, but you can read more about them here.
The jQuery
The jQuery part is actually pretty similar to my last article, all we’re going to do is change the XML node names for this particular API call… Create this in a file called controller.js :
$(document).ready(function() {
getFavorites();
});
function getFavorites()
{
var results;
$.get("/getFavorites.php", function(xml){
$('status',xml).each(function(i){
var title = $(this).find("text").text();
results = title + "<BR>";
});
$("#container").html(results);
});
}
All I have really changed is to iterate through each <status> node, and get each <text> child node from that. The <text> node holds the text of your favorite tweets. We’re also posting the results to a #container DIV again.
The HTML
This is actually exactly the same as the last tutorial, but i’ll re-post it here just for fun!
<HTML> <HEAD> <script src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js"></script> <script src="controller.js"></script> </HEAD> <BODY> <DIV id="container"></DIV> </BODY> </HTML>
There we go – done! Authenticating successfully to the Twitter API via PHP, with our jQuery happily rendering the XML returned via a GET ajax call and posting it back to our HTML document. The jQuery involvement is very loose in this example, as it really actually isn’t needed – but you get the idea on how you can integrate the ajax calls and build on up form there to do really cool things!
As always, here is the code to download to try it yourself
|
|
|
|
Similar Articles:
jQuery - PHP & Ajax with the Twitter API
Tech Bloggers Unite
Twitter API calls in the browser with JSON and jQuery
Sending Authenticated Data to the Twitter API
Chatterbloc revisited















Really nice tutorial.
This is really useful even if the info you are trying to get from Twitter does not require authentication, like an user’s timeline for example.
If your website is hosted on a big hosting company, you might not be able to get your feed (using http://twitter.com/statuses/user_timeline/cassianorabelo.xml for example) because of the limit of 100 calls to the API is IP based.
But if you use the technique you just taught the limit is no longer user based, but ID based. Thank you for sharing.
Great tutorial! Very useful for our school project.
If i had’ve knew this I wouldn’t have needed to hire a code to get my users authenticating so I could change their background !…:(..great article..
Nice tutorial and nice informative site you have here! I was wondering if you could explain how to split it out so the data you return from Twitter, if it has a link it, that the link would be active, and also include the @username of who posted it. In both the favs example and the Twitter search example.
Thanks!
-Jim
Good post. I’m going to try to play around with this stuff when I get home. I was looking to hire a coder to put together an auto background installer but I may be able to use what you have here to be able to write my own…hopefully!
hi
how to integrate twitter with codeigniter application, to get and update user status with oauth tech.
Thank you for this grat tutorial, i downloaded the code and for some reason when i try to test it on my local machine using XAMPP on windows, i am getting a blank page, i even copied-pasted the code but it is not working. Any help would be greatly appreciated
nb: i have cURL enabled