Send and load variables with AS3 and PHP

February 21, 2009 by: Christian

Because I use this all the time I decided to put the handling of loading variables with AS3 and PHP in a handy little class. The usage is really simple:

new VarLoader("yourscript.php",{var1:value1, var2:value2})

But before explaining everything in detail, lets look at this example:

flash-as3-php-send-load-variables

The complete AS3 code in varloader.swf:

import net.kaegi.loaders.VarLoader;
var vl:VarLoader;
//
sendBtn.addEventListener(MouseEvent.CLICK,sendBtnHandler);
function sendBtnHandler(e:MouseEvent) {
// Variables sent by POST-Method:
var varObj:Object = {};
varObj.textinput0 = escape(textinput0.text);
varObj.textinput1 = escape(textinput1.text);
// additionally another variable is sent by the GET method:
vl = new VarLoader("http://www.flashcmsframework.ch/wp-content/data/varloader/varloader.php?var_get=I was sent by GET!", varObj);
vl.addEventListener(Event.COMPLETE, onVarsLoaded);
vl.addEventListener(Event.CANCEL, onVarsCancel);
}
function onVarsLoaded(e:Event) {
var msg:String = "Communication with the server was successful.\n\n";
msg += "bulla -> "+e.target.vars.bulla+"\n";
msg += "gogull -> "+unescape(e.target.vars.gogull)+"\n";
msg += "var33 -> "+unescape(e.target.vars.var33)+"\n";
msg += "var_get -> "+unescape(e.target.vars.var_get)+"\n";
msg += "textinput0 -> "+unescape(e.target.vars.textinput0)+"\n";
msg += "textinput1 -> "+unescape(e.target.vars.textinput1)+"\n";
tf_servermsg.textColor = 0x009900;
tf_servermsg.text = msg;
}
function onVarsCancel(e:Event) {
tf_servermsg.textColor = 0x990000;
tf_servermsg.text = e.target.errormsg;
}

…and the code in varloader.php:


// coming from flash (GET)
$var_get = $_GET["var_get"];

// coming from flash (POST)
$textinput0 = $_POST["textinput0"];
$textinput1 = $_POST["textinput1"];

// internal
$bulla = "Blabla. I am just some lonely text.....";
$gogull = "WOW! I was fetched from the server as well! Cool!";
$var33 = "Hey, my name is 'var33'! What's yours?";

// result:
$result = "foo=bar";     // In AS3 the first variable MUST NOT have an '&', otherwise you will get an error message! So just use some dummy vars to make flash happy.
$result .= "&bulla=".rawurlencode($bulla);
$result .= "&gogull=".rawurlencode($gogull);
$result .= "&var33=".rawurlencode($var33);
$result .= "&var_get=".rawurlencode($var_get);
$result .= "&textinput0=".rawurlencode($textinput0);
$result .= "&textinput1=".rawurlencode($textinput1);

echo $result;

?>;

Download source code and example file

I hope someone can use this!

Update 12/28/2009: Some useful links regarding url encoding:
escape()
unescape()
encodeURI()
encodeURIComponent()
decodeURI()
decodeURIComponent()

Cheers,
Christian

Comments

24 Responses to “Send and load variables with AS3 and PHP”
  1. Sadaf says:

    Thanks for an excellent tutorial…

  2. admin says:

    @Sadaf
    You’re welcome! :)

  3. Rikk says:

    Thanks a lot, you just saved the life of one programmer.
    Your tut is full of win. +7 internets and kudos to you, man.

  4. Heidi says:

    Great! Thanks a lot.

  5. YaserKH says:

    vry tnx :)

  6. Rikk says:

    haha, one month later, your handy little class saved me again. Double thanx!!

  7. Ben says:

    Dude, you saved my life!

    Thank you very, very much.

    All the best!

  8. Christian says:

    Wow, I’m a lifesaver! That’s cool… ;-)

  9. Ruben says:

    Amazing post! thank you! :D

  10. Jake says:

    Super! You are quite generous. Thank you for the knowledge share!

  11. Dennis Saunders says:

    Thank you. It did the trick.

  12. Fabio says:

    Thanx, man! Thought in AS3 it wasn’t possible… good job!

  13. shortcut and cool. thanks

  14. Mann says:

    hi..
    nice work.
    how can load a php Array or object..?

  15. Christian says:

    You can serialize your arrays or objects and convert it in flash again by using “split”. For example like this:
    PHP: $result = “&myarray=”.rawurlencode($v1).”{“.rawurlencode($v2).”{“.rawurlencode($v3);
    AS3: var myarray:Array = e.target.vars.myarray.split(“{“)

    or, if more complex, use JSON.

  16. Nuthman says:

    Interesting, I wrote a AS3 to PHP class that does the same exact thing. check it out at
    http://www.as3blog.org/?p=18 ..This turns out to be a very useful class. Most of my site’s traffic comes to this link!

  17. Mann says:

    i need multidimensional Array loading.
    can i load through flashvars..?

  18. KEN says:

    真的相當實用!非常感謝您!

  19. Thanh Tung says:

    Hi, i did used, but i received error:

    Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: localhost:8080/c/varloader.php?var_get=I was sent by GET!
    at net.kaegi.loaders::VarLoader/init()
    at net.kaegi.loaders::VarLoader()
    at varloader_fla::MainTimeline/sendBtnHandler()

  20. Christian says:

    @thanh
    When you download the source files, open “varloader.fla” and publish it it’s working, right? You are testing it on the localhost, so there has to be something there or in the varloader.php itself.

  21. Sam says:

    Hey.. I cannot download the source codes for this example..

  22. Christian says:

    What exactly is the problem? I just checked it, it’s working…

  23. Sam says:

    Hi Christian,

    When I clicked “Download source code and example file” it directed me to a page which says “// You cannot download this file” and its url is “http://www.turtlebite.com/mint/pepper/tillkruess/downloads/tracker.php?url=http%3A//blog.turtlebite.com/wp-content/data/varloader/varloader.zip”.

    But I could download the files from “//blog.turtlebite.com/wp-content/data/varloader/varloader.zip”.
    Thanks for the excellent tutorial. I used LoadVars in AS2 and this is way advanced in AS3.

    Nice Work..

    Sam

  24. Christian says:

    Ah, I see, that’s just a redirect for statistics (haveamint.com). But if you could manage to download it now, that’s perfect, :-)

    Cheers,
    Christian

Trackbacks

Leave a Reply