Just Posted: Using xAuth, an alternate OAuth from Twitter

Authenticating Twitter API calls with PHP & jQuery

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 :)

Tools to help you learn:

Learning jQuery

Learning jQuery

Amazon Kindle 2

Amazon Kindle 2

jQuery Reference Guide

jQuery Reference Guide

16 Responses to “Authenticating Twitter API calls with PHP & jQuery”

  1. 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.

  2. Kerry says:

    Great tutorial! Very useful for our school project.

  3. 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..

  4. Jim says:

    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

  5. twitrounds says:

    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!

  6. ehsan says:

    hi

    how to integrate twitter with codeigniter application, to get and update user status with oauth tech.

  7. emma says:

    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

Trackbacks/Pingbacks

  1. [...] Authenticating Twitter API Calls with PHP and jQuery In this tutorial, you are shown how to authenticate the API calls to your Twitter credentials (to be able to return your favorites). [...]

  2. [...] Authenticating Twitter API Calls with PHP and jQuery In this tutorial, you are shown how to authenticate the API calls to your Twitter credentials (to be able to return your favorites). [...]

  3. [...] Authenticating Twitter API calls with PHP & jQuery Demo URL : View Demo. Description : In this tutorial you will learn how to make a simple call to the Twitter API with our your Twitter credentials (to return your favorites in this example) using PHP and jQuery. [...]

  4. [...] Authenticating Twitter API calls with PHP & jQuery Demo URL : View Demo. Description : In this tutorial you will learn how to make a simple call to the Twitter API with our your Twitter credentials (to return your favorites in this example) using PHP and jQuery. [...]

  5. [...] Authenticating Twitter API calls with PHP & jQuery Demo URL : View Demo. Description : In this tutorial you will learn how to make a simple call to the Twitter API with our your Twitter credentials (to return your favorites in this example) using PHP and jQuery. [...]

  6. [...] Authenticating Twitter API calls with PHP & jQuery Demo URL : View Demo. Description : In this tutorial you will learn how to make a simple call to the Twitter API with our your Twitter credentials (to return your favorites in this example) using PHP and jQuery. [...]

  7. [...] Authenticating Twitter API calls with PHP & jQuery Demo URL : View Demo. Description : In this tutorial you will learn how to make a simple call to the Twitter API with our your Twitter credentials (to return your favorites in this example) using PHP and jQuery. [...]

  8. [...] adapt this with lots more jQuery lovelyness! Feel free to share back your fine work! Also see my new article discussing making authenticated calls to the Twitter API. [...]

  9. [...] Authenticating Twitter API calls with PHP & jQuery 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… [...]

Leave a Reply