Michael Jay Lissner
  • Home
  • About Site
  • Contact
  • Projects & Papers
  • Tags
  • Archives

Privatizing the Twitter API Feed

UPDATE: Check the comments for a version with caching.

A friend of mine recently had a rather unfortunate event involving her twitter public timeline, so I thought the time had come to make mine private, more or less.

As a result, I needed to update the code that pulls my most recent Twitter posts into the left hand column so that it would authenticate using the Twitter API. Here’s the new code - it ain’t pretty, but it works:

<?php

    // Your twitter username & password.
    $username = "YOUR_USERNAME";
    $password = "YOUR_TWITTER_PASSWORD";

    //Concatenate the username and password
    $userpass = $username . ":" . $password;

    //Make up the feed URL
    $feed = "http://twitter.com/statuses/user_timeline.atom?count=1";

    //A function to parse the atom feed and pull out the useful info.
    function parse_feed($feed, $username) {
      $stepOne = explode("<content type=\"html\">", $feed);
      $stepTwo = explode("</content>", $stepOne[1]);

      $tweet = $stepTwo[0];
      $tweet = str_replace("&lt;", "<", $tweet);
      $tweet = str_replace("&gt;", ">", $tweet);
      $tweet = str_replace($username . ":", "", $tweet); 
      return $tweet;
    }

    //Create a curl object, give it the feed and authentication
    $curl_handle=curl_init();
    curl_setopt($curl_handle,CURLOPT_URL, $feed);
    curl_setopt($curl_handle,CURLOPT_USERPWD, $userpass);

    //Return the result, don't print it.
    curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER, 1);

    //Make the connection, set the variable, close the connection.
    $twitterFeed = curl_exec($curl_handle);
    curl_close($curl_handle);

    //Echo the parsed feed. Done.
    echo parse_feed($twitterFeed, $username);

?>

One dependency is the php-curl library, and after you install that, apache2 will want a restart.

I love getting feedback and comments. Make my day by making a comment.

Comments
comments powered by Disqus

  • « Zeran v. AOL Paper Posted
  • An Analysis of FTC Behavioral Advertising and an End of Semester Countdown »

Published

Mar 20, 2009

Category

Tech

Tags

  • blog 8
  • Twitter 4

Contact

This is Reader-Editable

Edit this post on Github

Get Weekly Updates

  • Unless mentioned otherwise, all material on this site is licensed under a Creative Commons copyright or the GNU Affero GPL. Privacy Policy.
  • Powered by Pelican. Theme: Elegant by Talha Mansoor