Loading Twitter Feeds Into Flash (Through PHP)

[swfobj src=”http://nathalielawhead.com/noodles/twitter.swf” height=”200″ width=”600″]
-Supports ability to retweet, follow, and shows a profile pic.

Download the sourcefiles here.

There are many cool things you can do with twitter. See Smashing Magazine’s article “50 Twitter Tools and Tutorials For Designers and Developers
I like it. Rather inspiring. Twitter is cool. You know you can even grab the coordinates of where the twitter twat tweeted?
I feel sinister seeing that. Consider making a “stalk your ex” app using Twitter and Google Maps where you can enter a user name and see your “ex’s” migration and travel habits.
Hehe… Evilness.
Importing Twitter feeds into Flash could be considered an easy task on the outset, and possibly was at one point, but (unfortunately) Twitter decided to cock-block Flash via their crossdomain policy (set HERE – view source )
Basically what happened is that any Flash based web application that accesses their data gets blocked. When this happened countless Twitter based Flash apps tanked causing a Flash wide panic to fix the problem. I know. Sucks to be you.
The way to talk to api.twitter.com these days (twitter api = entirely HTTP based) is to call a script on your server and then proxy the request to twitter… aaaand you can still load your RSS feed via making an AS call to everyone’s favorite handy language; PHP!
I found many of the AS libraries out there to be a bit poorly documented, abandoned, one-man-show-ish, or rather “scattered”… Looking for solutions that’ll do the work for you lead to a lot of dead ends. Many people took the approach of “going it their own”, and just doing it the classic way of Flash > PHP > Twitter Feed. It’s not that hard once you understand the system.
You can use php, javascript, or any other intermediary language for that matter, to return data back to your Flash app, thwarting Twitters seemingly devious scheme of cock blockery. So ha!
There are a number of ways out there. Like THIS ONE, that uses Yahoo Pipes — plugging your twitter ID into a pipe and pulling in the URL for the feed. I like it. It’s an interesting approach.

Nevertheless… Here’s my shot at importing teh Twitters:


//holds xml data
var twitterXML:XML;
//Twitter id
var myTwitterID:String = "alienmelon"; //or 44314795
//The url to the PHP file
var phpURL:String = "http://nathalielawhead.com/dev/gateway.php";
//XML_loadInit by passing it url to Twitter info:
//XML_loadInit("http://twitter.com/statuses/user_timeline/" + myTwitterID + ".xml?count=5");
XML_loadInit(phpURL+"?twitterId="+myTwitterID);

…etc.

The twitter ID is your username. There are two ways to use it, as the set of numbers OR just as the classic username. To convert your user name to numbers you can use: ID From User
It’s handy… in the event you like your shit cryptic.

The rest goes like so:


function XML_loadInit(URL:String):void
{
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, XML_onLoadComplete);
urlLoader.load(new URLRequest(URL));
}

function XML_onLoadComplete(e:Event = null):void
{
//populate
twitterXML = new XML(e.target.data);
populateTwitter();
//clear listener
e.target.removeEventListener(Event.COMPLETE, XML_onLoadComplete);

}

function populateTwitter():void
{

//number of post to display
var postNum:Number = 0;
var username:String = twitterXML.status.user.name[postNum];
var userurl:String = twitterXML.status.user.url[postNum];
//populate the text field with the FIRST element in status.text nodes
//you could do multiple by looping or display a random one...
//Basic version is: txt_twitter.htmlText = twitterXML.status.text[postNum];
txt_username.htmlText = username;
txt_url.htmlText = userurl;

//clickable feed that takes you to that one post
txt_twitter.htmlText = "< font color='#224466' > < a href = " + '"http://twitter.com/' + myTwitterID + "/statuses/" + twitterXML.status.id[postNum] + '" target= "_blank" > ' + twitterXML.status.text[postNum];

//Load Profile Pic - setup and load
url = encodeURI(twitterXML.status.user.profile_image_url[0]);
var urlReq:URLRequest = new URLRequest(url);
picLdr.load(urlReq);
addChild(picLdr);
//set xy to whatever...
picLdr.x = 410;
picLdr.y = 8;

}

…Basically that’s it.

Also, one major error I kept running into while I was putting together my go at it was:

TypeError: Error #2007: Parameter text must be non-null...

This was after creating the .php file that serves as the “twitter gateway” a.k.a. “gitterway”.

Searching for it I noticed a number of other people who where doing what I was doing suffering from the same error illness. I was nearing death trying to figure it out, so I’m making mention of how I solved it. Hope it’ll help whoever googles and finds this…
It’s an ambiguous error which can mean many things.
In this case the culprit was the php function “file_get_contents” which is considered a security risk with some host providers and thus “disabled” so substitute that for curl.

Like so:


< ?php $twitterId = isset($_REQUEST["twitterId"]) ? $_REQUEST["twitterId"] : ''; if( $twitterId == "" ){ exit; } $c = curl_init("http://twitter.com/statuses/user_timeline/" . $twitterId . ".xml"); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($c); curl_close($c); echo $response; ?>

This works. Upload it and point your script to it.

It also never hurts to throw in a crossdomain.xml. Which solves many other problems encountered while working with Twatter.


< cross-domain-policy >
< allow-access-from domain="*twitter.com" / >
< /cross-domain-policy> < /code >

...Like a boss.

Another problem with Twitter (if you are going to be pulling in tweets into Flash at a "larger" basis) is their 100 pings-per-hour policy. Which is really the biggest problem if this is going to be used for commercial productions.
You can request to be "whitelisted" so this is bypassable (if you give them a good reason for whitelisting):
http://twitter.com/help/request_whitelisting
Whitelisting enables to make up to 20,000 requests per hour.
http://dev.twitter.com/pages/api_faq#rate_limit
But they are known to turn people down (often... Nazi often). SO the only other option left is to use cookies or something like SharedObject to "Flash cookie" your content to implement some sort of "ping restriction" to put a cap on how often a user loads a fresh feed through your Flash app... or by some other, more cleaver means.
I'm up to challenge myself with that issue next, although testing it should be... um... interesting.

DOS interesting...

Hehe.

Grab a fla.