Been spending most of my time on Flux 112 lately, and one of my duties is to track data in real time within Flash. After much trial-and-error, i’ve found that getting data from PHP is actually really easy, but there aren’t many great tutorials out there, so here is how i do it.
First use this function to send a query to the PHP script:
private function sendQuery():void
{
var variables:URLVariables = new URLVariables();
variables.username = "Bruce Willis";
var request:URLRequest = new URLRequest("http://yoursite.com/the_script.php");
request.method = URLRequestMethod.POST;
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, querySent);
try
{
loader.load(request);
}
catch (error:Error)
{
trace("Upload failed.");
}
}
URLVariables are what you send to the PHP script. I am sending a String here, but you can send Numbers, pictures, etc.
Next, we create the URLRequest, which we will send through a URLLoader. Set the URLRequestMethod to “POST” (i will explain why later) and give the request whatever data it should send (our previously created variables).
Then, create the URLLoader (you can send the request to the constructor, or leave it blank and load it like i did below). I like to set the DataFormat to “TEXT”, but this is not always necessary. Add an EventListener to that loader, so that you can execute a function when the request was completed. And finally, load the request! I use a “try / catch” statement in case there is an issue, and trace out whatever error message you want.
Then comes the function that is called when the loader is complete:
private function querySent(e:Event):void
{
if (!e.target.data)
trace("No data back");
else
trace("The most awesome actor is: " + e.target.data);
}
This one is simple: when the loader is complete, it first checks to see if any data came back from the request (line 3). If so, it traces the data you sent (in this case, “Bruce Willis”).
Here is the PHP script, called “the_script.php” and saved on your website.
<?php
/**
* @author: Maxime Preaux
*/
// get the variable from AS3
$name = $_POST["username"];
if (!$name)
{
echo("no data sent from Flash");
}
else
{
echo($name);
}
?>
First, a PHP script starts with “<?php” and ends by closing that tag: “?>”.
Then, I declare the variable like so: “$name”. It’s that simple. Then, I assign the value of $_POST["username"] to that variable. In PHP, you can either use “GET” or “POST” to send data to a script. The “GET” method actually writes the data in the browser bar, whereas the “POST” method keeps it hidden. In our case, either method would work.
Make sure that ["username"] matches the variable sent from AS3 (see line 4. of the first AS3 script).
After that, we check to see if $name has a value. If it got the data from AS3 correctly, it should! Otherwise, it will send “no data sent from Flash” back to your program. But if all works correctly, it will send the value of username back to you using the “echo()” function.
And that’s it! You now know how to send and get data from AS3 and PHP!
If you have any questions or better ways to do this, please post in the comments below