Have you ever had the urge to roll your own twitter? Well I have and I have tried a number of methods to come to the conclusion I needed to write everything from scratch. So I did and I wouldn’t say that I wasn’t without help. I did study Lee Brimelow’s gotoandlearn.com ZendAMF tutorial. So I went about downloading and install zend on my server and I found it was extremely easy to use. One of the many things that attracted me to the Zend framework were the prebuilt services. The zend framework is a mature framework that has a great deal to offer developers so I will briefly show you a simple amf flash gateway built with zend to fetch the twitter service. Also please note you will have to create your own classes if you want to extend this but it should be pretty easy to do considering..

So first off download the zendframework. Configure your zendAMF endpoint.
Create your own ZendAMF endpoint:
<?php error_reporting(E_ALL|E_STRICT); ini_set("display_errors", "on"); ini_set("include_path", ini_get("include_path") . ":/home/toYourZend/FrameWorkHome/frameworks/zendFramework/library/"); require_once('Zend/Amf/Server.php'); require_once('TwitService.php'); $server = new Zend_Amf_Server(); //adding our class to Zend AMF Server $server->setClass("TwitService"); echo ( $server -> handle() ); ?>
Now configure your zend Twitter Service
Create Your Twitter Service Class:
<?php error_reporting(E_ALL|E_STRICT); ini_set("display_errors", "on"); ini_set("include_path", ini_get("include_path") . ":/home/toYourZend/FrameWorkHome/frameworks/zendFramework/library/"); require_once ('Zend/Service/Twitter.php'); require_once('VOgetFriendsTimeline.php'); //connection info class TwitService { public $tmp; public $twitter; public $response; public function __construct() { } public function getfriendsTimeline() { $twitter = new Zend_Service_Twitter('YourUserName', 'YourPassword'); $response = $twitter->status->friendsTimeline(); $tmp = new VOgetFriendsTimeline(); for ($i = 0; $i <= 18; $i++){ // array_push($tmp->userTwit, $response->status[$i]); array_push($tmp->userTwit, (string)$response->status[$i]->user->screen_name); array_push($tmp->statusPic, (string)$response->status[$i]->user->profile_image_url); array_push($tmp->locTwit, (string)$response->status[$i]->user->location); array_push($tmp->friendsTimeline, (string)$response->status[$i]->text); } return $tmp; } public function UserPostUpdate($tweet){ $twitter = new Zend_Service_Twitter('YourUserName', 'YourPassword'); $response = $twitter->status->update($tweet); } }
Now configure your flash variable object that is exposed by the service.
<?php class VOgetFriendsTimeline { public $user_twit; public $pass_twit; public $locTwit = array(); public $friendsTimeline = array(); public $statusPic = array(); public $userTwit = array(); public $text; public function __construct() { } }
Finally configure your flex project to consume the service.
<?xml version="1.0" encoding="utf-8"?> <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="600" height="300" initialize="init()"> <mx:Script> <![CDATA[ import com.SampleTitleWindow; import mx.events.MenuEvent; import mx.controls.Alert; import mx.collections.*; import mx.managers.PopUpManager; import mx.controls.Alert; import nl.demonsters.debugger.MonsterDebugger; import flash.net.*; public var nc:NetConnection = new NetConnection(); public var resp:Responder = new Responder(onResult, onError); public var resp1:Responder = new Responder(onError); public function init() { nc.objectEncoding = ObjectEncoding.AMF3; nc.connect("http://yoursite.com/toYour/zendEndpointAMF/index.php"); trace("I was ran!"); nc.call("TwitService.getfriendsTimeline",resp); } public function onResult(e:Object) { trace("Recieved Object..."); //userPosts.dataProvider = e.friendsTimeline; var arr:Array = []; for (var i:int = 0; i < e.friendsTimeline.length; i++ ) { var obj:Object = {}; obj.text = e.friendsTimeline[i]; obj.from = e.userTwit[i]; obj.pic = e.statusPic[i]; arr.push(obj); } trace(e); datagrid.dataProvider = arr; } public function zendPost() { var tweetPost:String = new String(); tweetPost = twitPost.text; nc.objectEncoding = ObjectEncoding.AMF3; nc.connect("http://yoursite.com/toYour/zendEndpointAMF/index.php"); trace("I was ran!"); nc.call("TwitService.UserPostUpdate",resp1,tweetPost); } public function onError(e:Object) { trace("And the Error is !!!!"); trace(e); } ]]> </mx:Script> <mx:VBox width="100%" minWidth="600" height="100%"> <mx:VBox width="100%" minWidth="600" height="80%" verticalAlign="top" horizontalAlign="center"> <mx:DataGrid id="datagrid" width="100%" minHeight="150" maxHeight="200"> <mx:columns> <mx:DataGridColumn headerText="Tweet" dataField="text" wordWrap="true" /> <mx:DataGridColumn headerText="Avatar" width="150" id="imgAvatar" itemRenderer="mx.controls.Image" dataField="pic" /> <mx:DataGridColumn headerText="@user" width="150" dataField="from"/> </mx:columns> </mx:DataGrid> </mx:VBox> <mx:VBox width="100%" verticalAlign="top" horizontalAlign="center"> <mx:HBox width="98%" verticalAlign="middle" height="10%" horizontalAlign="center"> <mx:TextArea id="twitPost" width="90%" horizontalCenter="true" ></mx:TextArea> <mx:Button x="10" y="10" label="Post" click="zendPost()" /> </mx:HBox> </mx:VBox> </mx:VBox> </mx:Canvas>
Thats it! View the live example here!
Other Cool Twitter Mashup links can be found:
Twitter MashUp Links
Don’t forget to check out Lee Brimelow’s GotoandLearn AMF video.
Zend framework
Zend Services
Related posts:













