Accessing Tweets Using Flex.


This tutorial  will explain how you can access tweets of a user using his/her twitter username .

Things you need

1) Flex sdk to build the file

2) Notepad

3)Twitter account to access tweets :p

There are two part you need a php file and a flex counter-part
Why do i need php?? hold on guys!!!!!.

If i try to access tweets directly from flex it throws an security error which is why i call a php to retrieve all the data and send it back to flex.In the flex side i’m just populating the data into a datagrid. you can use better ui once u get the basic.
This is the logic flow
1-> enter the twitter user id
2-> using the user id i pass it to the php code using httpservice of flex. in the
php with the help of twitter api get the rss feeds for that user.
(the php code for that is show below .i’ve put the file in my html directory and the file name is tweet.php)

tweet.php

<?php
$username = $_GET[‘user_id’];
$twitter_feed = “http://twitter.com/statuses/user_timeline/”.$username.”.rss”;
$rawfeed = @file_get_contents($twitter_feed);
print $rawfeed;
?>

3-> As you can see i’m getting the userid and constructing the api to get the rss
feed of that user
4->Once done i pass the rss to the flex .

5->Once inside flex its straight forward i just need to populate the datagrid with
the values.

Below is the FLEX code.

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” width=”100%” height=”100%” creationComplete=”init()”>
<mx:HTTPService id=”rssParse”
url=”http://localhost/tweet.php”
result=”r_Handler(event)” fault=”error(event)”>

</mx:HTTPService>

<!– Now lets set up the controllers for this application –>
<mx:Script>
<![CDATA[
import mx.managers.PopUpManager;
import mx.rpc.events.FaultEvent;
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;

[Bindable]
private var f_Array:ArrayCollection;

private function init():void{
}

private function onGetUser():void{
var obj:Object = new Object();
obj[‘user_id’] = userId.text;
rssParse.send(obj);
}

private function r_Handler(event:ResultEvent):void
{
if(event.result.rss == null){
Alert.show(“User id may be wrong”);
}
f_Array = event.result.rss.channel.item as ArrayCollection;
}

private function f_Handler(event:FaultEvent):void
{
Alert.show(event.message.toString(),”Error”);
}
private function error(error:FaultEvent):void{
Alert.show(“oppoapsaospaospo”);
}

]]>
</mx:Script>
<mx:VBox width=”100%” height=”100%”>
<mx:HBox>
<mx:Label text=”Enter Twitter User Id”/>
<mx:TextInput id=”userId”/>
<mx:Button label=”Go” click=”onGetUser()”/>

</mx:HBox>
<mx:DataGrid x=”0″ y=”56″ width=”100%” height=”100%” dataProvider=”{f_Array}”>
<mx:columns>

<mx:DataGridColumn headerText=”Title” dataField=”title”/>
<mx:DataGridColumn headerText=”Date” dataField=”pubDate”/>
</mx:columns>
</mx:DataGrid>

</mx:VBox>
</mx:Application>

Please leave a comment or a query i’ll be happy to help you.