jump to navigation

Two way communication between Flash and PHP (exchanging variables) September 18, 2010

Posted by Tournas Dimitrios in Flash.

It’s not that difficult to post variables to PHP using Actionscript 3 and by doing so you can communicate back and forth between your Flash application/Game and PHP. This is the a simple way I know  to communicate with PHP and Actionscript 3 and it’s extremely fast and efficient as well. Although the number of lines required to perform a simple request is considerable, you can easily create yourself a class wrapper which will help out and make this task shorter and simpler.

The basic code to post variables to PHP is as follows :

import flash.net.*;

//the URLLoader
var urlLoader:URLLoader;

//holds the variables to send to PHP
var urlVariables:URLVariables = new URLVariables();

//let's add some variables to send to PHP
urlVariables.email = "foobar@somewhere.com";
urlVariables.password = "foo_bar";
urlVariables.from_flash = "from_flash";

//holds the url address
var url:String = "http://yourdomainhere.com/php_flash_test.php";

//holds a URLRequest
var urlRequest:URLRequest = new URLRequest(url);

//specify post as the method to send
urlRequest.method = URLRequestMethod.POST;

//set the variables to be used for the data to send
urlRequest.data = urlVariables;

//initialize the urlLoader
urlLoader = new URLLoader();

//specify to use variables as the data format
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;

//make the actual request to the php page now

//setup listeners for handling the onComplete and error handlers
urlLoader.addEventListener(Event.COMPLETE, onCompleteHandler);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onErrorHandler);

function onCompleteHandler(e:Event):void {
//get the returned variables
var variables:URLVariables = new URLVariables(e.target.data);

if(variables.msg == "do this") {
output_txt.text = "do this \n";
output_txt.appendText("Username: " + variables.username + "\n");
output_txt.appendText(" Other data: " + variables.otherdata);
if(variables.msg == "do that") {
output_txt.text = "do that \n";
output_txt.appendText("Username: " + variables.username + "\n");
output_txt.appendText("Other data: " + variables.otherdata);

function onErrorHandler(e:IOErrorEvent):void {
trace("ioErrorHandler: " + e);

The above code works for simple requests and is extremely fast performance wise compare to using XML or even AMFPHP. Like I said,t he code is rather long but you can easily wrap it up nicely in a simple class and perhaps make a send() method to make things even nicer.

Note: If you forget to import flash.net.* at the beginning of your actionscript code, the code will not work correctly.

Below is the PHP file that you can place on your server if you want to test the above example.


if($_POST['from_flash']) {
//$array['msg'] = "do this";
$array['msg'] = "do that";

//assign other variables as well to pass back to flash
$array['username'] = "ricky";
$array['otherdata'] = "Other data can be put here";

if(isset($array)) {
echo http_build_query($array);

The whole reason I’m posting the from_flash variable in the first place is just simply to detect if the page is being loaded from flash or not. If the page is loaded up directly, it will be blank. If the page is POSTED to from our example Actionscript 3 code from above, then it will contain the from_flash variable and will enter into our php code nicely. I’ve also put a check in there to see if $array is empty before trying to build the query to echo out to the screen which also doesn’t output if you visit the page directly on your server.

This whole thing about php returning nothing to users that visit the page directly isn’t for security but rather just in case someone accidentally visits the page, no php errors will be generated and the user will also not see any funny output in their browser.

Important note: I only used $_POST[‘from_flash’] variable in the php file but you actually have access to the other posted variables $_POST[’email’] and $_POST[‘password’] all of which we sent from flash of course.
If your looking for an already premade class to encapsulate all of this for you, then you might wan to check out this blog post here.


1. rahul - February 15, 2014

Thanks a ton.. I have searched many websites for this integration. But this is completely helpful for me… thanx very much

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s