When character counts are critical as in building a twitter or SMS app you could use services such at tinyurl.com and bit.ly to help save text real-estate. I prefer bit.ly since it’s domain has less characters then tinyurl’s
The code is really quite simple. All that you need to do is communicate with bit.ly ‘s API and it returns the new shortened URL via its API. Check out http://bit.ly/docs for full API.
original call
http://bit.ly/api?url=URL (Example: http://bit.ly/api?url=http://twitabit.com)
bit.ly response:
http://bit.ly/2oCfqc
Seems easy enough, but the problem comes when doing this with in Flex. You will quickly see once you test on your server Flex will alert you that there is a Sandbox violation. This simply means that bit.ly does not have a crossdomain.xml file that grants you permission to receive data from them.
In order to receive this data i checked out this great post Using a PHP Proxy with Flex to talk to Cross Domain
proxy.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php $ch = curl_init(); $timeout = 30; $userAgent = $_SERVER['HTTP_USER_AGENT']; curl_setopt($ch, CURLOPT_URL, $_REQUEST['url']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); $response = curl_exec($ch); if (curl_errno($ch)) { echo curl_error($ch); } else { curl_close($ch); echo $response; } ?> |
Flex code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | <?xml version="1.0" encoding="utf-8"?> <mx:Application layout="vertical" xmlns:mx="http://www.adobe.com/2006/mxml" viewSourceURL="srcview/index.html"> <mx:Script> <![CDATA[ import mx.rpc.http.HTTPService; import mx.rpc.events.ResultEvent; import mx.rpc.events.FaultEvent; import mx.controls.Alert; public function shortenURL(url:String):void { /******************* * Build API REST call ********************/ var newURL:String = "http://bit.ly/api?url="+url; /******************* * PROXY DATA FROM BIT.LY ********************/ var proxy:String = "proxy/proxy.php"; var request:HTTPService = new HTTPService(); request.url = proxy; request.method = "POST"; request.addEventListener(FaultEvent.FAULT, handleFault); request.addEventListener(ResultEvent.RESULT, handleResult) /******************* * Pass HTTP to PHP so that PHP could * handle the request and avoid cross domain error ********************/ var param:Object = new Object(); param.url = newURL; request.send(param); } /******************* * PLACE RESULT ACTION HERE ********************/ private function handleResult(e:ResultEvent):void{ shortURL_txt.text = e.result.toString(); } private function handleFault(e:FaultEvent):void{ Alert.show(e.fault.rootCause.toString()); } private function handleClick(evtObj:Event):void{ shortenURL(url_txt.text.toString()); } ]]> </mx:Script> <mx:Label text="Original URL" /> <mx:TextInput id="url_txt" text="http://blog.unthinkmedia.com" /> <mx:Label text="Shortened URL" /> <mx:TextInput id="shortURL_txt" /> <mx:Button click="handleClick(event)" label="send to bit.ly" /> </mx:Application> |
Bit.ly and Flex Example | Source
Popularity: 2% [?]
AS3, bit.ly, cross domain, example, Flex, PHP, source










or better, flex specific: http://blog.unthinkmedia.com/?p=15
That is very attention-grabbing, You’re an excessively skilled blogger. I have joined your rss feed and look forward to looking for more of your fantastic post. Additionally, I’ve shared your website in my social networks