I’ve been working on a project which has now required me to interact with Facebook. There are several ActionScript APIs out there that will interact with Facebook, however, I don’t want to embed my secret Facebook developer key into a SWF which can be easily decompiled. So what I’m going to do is have Flash talk to PHP which in turn can use the official Facebook PHP API to make the calls (basically a proxy system) to Facebook on Flash’s behalf. What I’ll show today is a very fast (and basic) way to do this.
Please note that this article isn’t in the tutorial line I usually do, so I won’t be going over every nitty gritty detail. I just want to slam some code out there for people to have a starting point to build upon.
To start I’m using Flash CS4 although Flash CS3 and Flex 3 can both do this as well with very minor modifications. In Flash I created a new “Flash File (ActionScript 3)” project and set the document properties to 400px wide by 150px in height. I then used the Rectangle tool and sized it to the size of document and set the background color to: #ECEFF6 and then I made a border around it with the width set to: 2 and the color to: #D4DAE8. Highlighting the entire rectangle I then turned it into a movie clip. From there I added a few layers to the timeline to separate my one component, the background, and the ActionScript. I then added the following ActionScript file to my project:
package
{
import flash.events.*;
import flash.net.*;
import fl.controls.TextArea;
public class URLLoaderFB
{
var txt:TextArea = null;
public function URLLoaderFB(textArea:TextArea)
{
var loader:URLLoader = new URLLoader();
txt = textArea;
configureListeners(loader);
var request:URLRequest = new URLRequest("http://www.YOUR_WEB_ADDRESS_HERE.com/fb_services.php?FB_GET_USER_INFO");
try
{
// We could add some parameters to pass to PHP here ...
request.method = URLRequestMethod.GET;
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.load(request);
}
catch (error:Error)
{
txt.htmlText = txt.htmlText + "<br/>Unable to load.";
}
}
private function configureListeners(dispatcher:IEventDispatcher):void
{
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
private function completeHandler(event:Event):void
{
var loader:URLLoader = URLLoader(event.target);
txt.htmlText = txt.htmlText + "<br/>" + loader.data;
}
private function openHandler(event:Event):void
{
txt.htmlText = txt.htmlText + "<br/>openHandler: " + event;
}
private function progressHandler(event:ProgressEvent):void
{
txt.htmlText = txt.htmlText + "<br/>progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal;
}
private function securityErrorHandler(event:SecurityErrorEvent):void
{
txt.htmlText = txt.htmlText + "<br/>securityErrorHandler: " + event;
}
private function httpStatusHandler(event:HTTPStatusEvent):void
{
txt.htmlText = txt.htmlText + "<br/>httpStatusHandler: " + event;
}
private function ioErrorHandler(event:IOErrorEvent):void
{
txt.htmlText = txt.htmlText + "<br/>ioErrorHandler: " + event;
}
}
}
This does nothing more than make the call out to my PHP server which in turn will call into Facebook to make the call to users.getInfo.
Keep in mind you need to load the parameters that Facebook passes to you back to the PHP page. My code above doesn’t show that. In Flex, the code would look like this:
// Get the fb_sig_session_key Application.application.parameters.fb_sig_session_key // Get the fb_sig_user Application.application.parameters.fb_sig_user // TODO: Make sure these are passed to the PHP page, they could for example be passed on the querystring.
In Flash I dropped a textarea control onto the movie clip I made earlier and give it an instance name of “output_txt”. Now just add the following code in the movie clip I created earlier:

Quick and dirty code at it’s best and I’m simply passing in the textarea’s instance name and letting the URLLoaderFB class handle the resulting output (good or bad).
At this point you will need to sign into to Facebook and mark yourself as a developer. At that point you can create an application if you want to test this all out.
Moving onto the PHP side of things you need to make sure you have a PHP server that can be accessed from Facebook. Next you will need to download and install the Facebook PHP Client API files. I then crafted a PHP file called “fb_services.php” with the following contents:
<?php
require_once 'facebook.php';
$appapikey = 'YOUR_APP_API_KEY_HERE';
$appsecret = 'YOUR_APP_SECRET_KEY_HERE';
$facebook = new Facebook($appapikey, $appsecret);
// These following request variables need to be set in the ActionScript code
$facebook->set_user($_REQUEST['fb_sig_user'], $_REQUEST['fb_sig_session_key']);
if(isset($_GET['FB_GET_USER_INFO']))
{
$user_details = $facebook->api_client->users_getInfo(array($_REQUEST['fb_sig_user']), array('uid', 'pic_square', 'profile_url', 'name', 'locale'));
// I'll return this as JSON, so you would need to remember that on the Flash/Flex side if you
// wanted to take this further
echo json_encode($user_details);
}
?>
Now, upload your SWF to the place where you have the fb_services.php file hosted at as well as the Facebook PHP API files and you should be able to test this out by going to your project inside of Facebook.
I’m sure this can be easily improved upon, but at this point I’m a Facebook newbie developer so it is what it is! Have fun.




Hi nice work, can u send me the code for as2
@Noman,
I don’t work with AS2 anymore and haven’t for a couple years now. It shouldn’t be too hard though for a person to move this code to AS2. I simply don’t have the time right now to do it myself.
Pingback: Getting Perl and Flex talking via XML « Giant Flying Saucer
Nice trick here Ok now, i just want to say that you’ve put up ainformative article. Thanx for this and Good luck on writing tons more like these.