En este ejemplo, ya no muy básico, les mostrare como obtener los últimos tweets de un usuario y mostrarlos en Flex 4 (Gumbo) para ello haremos uso de PHP como gateway, con PHP obtendremos los tweets por medio del API de Twitter y los desplegaremos en forma de XML para poder leerlos en Flex.
Ejemplo
Lo primero que necesitamos es crear el script en PHP para poder conectarnos al API de Twitter, obtener los tweets y desplegarlos en XML.
PHP
[php]
<?php
/**
* get_user_tweets()
*
* @param mixed $username
* @param integer $count
* @return array $out
*/
function get_user_tweets($username, $count = 10)
{
/**
* URL para obtener los tweets en formato JSON
* Utilizaremos curl para hacer la conexion al API de Twitter
* */
$url = ‘http://twitter.com/statuses/user_timeline/’.$username.’.json?count=’.$count;
$curl = curl_init();
/**
* Iniciamos CURL pasando que URL vamos a cargar
* */
curl_setopt($curl, CURLOPT_URL, $url);
/**
* Indicamos que querremos el output de regreso
* */
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
/**
* Ponemos un TimeOut al script
*/
curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, 10);
/**
* Ejecutamos CURL
*/
$json = curl_exec($curl);
/**
* Cerramos la conexion
*/
curl_close($curl);
/**
* Tomanos el resultado (JSON) y lo parseamos en PHP
*/
$tweets = json_decode($json);
$out;
/**
* Por ultimo por cada tweet tomanos el contenido y lo metemos en un Array
*/
foreach($tweets as $tweet)
{
$out[] = $tweet->text;
}
return $out;
}
/**
* Con la informacion obtenida del API construimos un XML y lo mostramos
*/
$out = ‘<?xml version="1.0" encoding="utf8"?>’;
$out .= "<tweets>";
$tweets = get_user_tweets(‘tmeister’, 10);
foreach($tweets as $tweet)
{
$out .= "<tweet>$tweet</tweet>";
}
$out .= "</tweets>";
header ("content-type: text/xml");
echo $out;
?>
[/php]
El resultado del script lo puedes ver en http://tmeister.dev/flex/gumbo/last-tweet/last.php
MXML
Una vez que tenemos el XML solo falta mostrar su contenido en Flex.
[xml]
<?xml version="1.0" encoding="utf-8"?>
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
width="100%"
height="100%"
creationComplete="{service.send()}"
viewSourceURL="http://tmeister.dev/flex/gumbo/last-tweet/srcview/index.html"
>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
[Bindable]
private var _tweets:ArrayCollection;
protected function service_resultHandler(event:ResultEvent):void
{
_tweets = new ArrayCollection();
for each( var tweet:String in event.result.tweet )
{
_tweets.addItem(tweet);
}
}
protected function service_faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.faultString, "Error");
}
]]>
</fx:Script>
<fx:Declarations>
<s:HTTPService
id="service"
result="service_resultHandler(event)"
fault="service_faultHandler(event)"
url="http://tmeister.dev/flex/gumbo/last-tweet/last.php"
showBusyCursor="true"
resultFormat="e4x"
>
</s:HTTPService>
</fx:Declarations>
<mx:VBox verticalCenter="0" horizontalCenter="0">
<mx:Repeater id="rep" dataProvider="{_tweets}">
<s:Label text="{rep.currentItem}" />
<mx:HRule width="100%" />
</mx:Repeater>
</mx:VBox>
</s:Application>
[/xml]
Con esto ya podemos mostrar los últimos tweets de un timeline sin necesidad de usar librerias ni de PHP o de ActionScript.
Leave a Reply