AS
and PHP Made Easy
         by laurent hogue

During my past years, I developed several applications for PC and Mainframe Computers on different languages - from ASM to java. But recently, I discovered the wonderful world of the ActionScript.

Seeking for some good tutorials on the net, I always found poorly-written tutorials, or in other cases, examples far too complex for beginners. So today, I will show you, with a generic example, how to communicate data back and forth from AS and PHP.

PART 1 - HOW DOES IT WORK
Before starting programming like a devil, let's try to understand how ActionScript and PHP can communicate. Suppose that you want to send a written letter to your best friend. You need his postal address, an envelope, and something to send such as the content or pages in your letter.

First, you write the different pages of your letter. Next, you put those pages in the envelope, and finally you post it. Do you understand?

Good. You now know the concept. It was easy? I know. Programming in AS and PHP works in a similar fashion. Let's try to program it.

PART 2 - ACTIONSCRIPT
In AS, there is a kind of variable called "LoadVars". It is the equivalent of the envelope in our letter example above. Before putting your valuable content in it,
you have to first create it.

$envelope = new LoadVars();

Next, you have to put what we called before your "pages". In fact, these
are the variables of your code that you want to pass to PHP. How do you
do it? Let me show you:

$var1 = "hello, I'm Bobby.";
$var2 = 100;
$var3 = "John Deer";

$envelope.variable1 = $var1;
$envelope.hellothere = $var2;
$envelope.byebye = $var3;

The name that you declare after $envelope (in our example, variable1, hellothere, and byebye) is the name of the variable that you will fetch in PHP.

Now that you put your content in your envelope let's post it ! How do we do that? By calling a special function specific to the LoadVars variable.

          $envelope.send("http://www.mydomain.com/script.php");

Here we go ! You are currently sending your envelope to "script.php". It was easy, don't you think?


PART 3 - PHP
Now, we have to catch the informations sent by the AS. I'm assuming that your PHP version is 4.x. If not, change your PHP version or Hosting company, they are not serious.

To retrieve the info...

$phpvar1 = $HTTP_POST_VARS["variable1"];
$phpvar2 = $HTTP_POST_VARS["hellothere"];
$phpvar3 = $HTTP_POST_VARS["byebye"];

?>

You see, you are fetching the variables sent from AS by putting the content of the php array $HTTP_POST_VARS at the index called by the name of your variable. Now, these sentences in PHP are true :

$phpvar1 == "hello, I'm Bobby."; //true
$phpvar2 == "100"; //true
$phpvar3 == "John Deer"; //true

Voila ! It wasn't difficult at all, don't you think?


PART 4 (optional) - HOW TO SEND AND RECEIVE
Suppose now that you want to send and receive information/data. How do you do that? The following example shows how:

//Actionscript
$var1 = "hello, I'm Bobby.";
$var2 = 100;
$var3 = "John Deer";

$envelope = new LoadVars();
$envelope_received = new LoadVars();

$envelope.variable1 = $var1;
$envelope.hellothere = $var2;
$envelope.byebye = $var3;

$envelope.sendAndLoad("http://www.mydomain.com/script.php",
$envelope_received);

//----------------------------------



What we have done is that we have put content in our first envelope but told AS to put the content read from PHP in the variable $envelope_received. This variable MUST be of LoadVars type.

How do you send information from php? Behold:

$phpvar1 = $HTTP_POST_VARS["variable1"];
$phpvar2 = $HTTP_POST_VARS["hellothere"];
$phpvar3 = $HTTP_POST_VARS["byebye"];

$mycredits = "Laurent Hogue";

echo "status=complete&final=yes";
echo "&credits=".$mycredits;
?>



The "echo" function in PHP is used to send what is written after it to an output channel. This is what AS will receive. It works like a typewriter: the first echo is sent to the output channel, the next echo will be written in the output channel after the first one. If you put a string followed by a dot and a variable name, you concatenate the string into:

"&credits=".$mycredits == "&credits=Laurent Hogue"; //true

Dont forget - in PHP, you must separate each variable with an & - as
described in the echo example above.

The Final Step - Reading the PHP Data in AS
It's simple, in fact. The values are stored the same way they are in the envelope. So to follow our example, you can fetch the information coming from PHP like this:

//Actionscript

$the_status = $envelope_received.status;
$final_result = $envelope_received.final;
$tut_credits = $envelope_received.credits;

//----------------------------------



Therefore, the values afterward equal:

$the_status == "complete"; //true
$final_result == "yes"; //true
$tut_credits == "
Laurent Hogue"; //true

 

 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.