Flash/PHP Communications

Interaction Strategies

 

 

One-Way Communications

PHP and FlashVars

The first example is how to use PHP directly to feed one-way data that is processed by Flash. By using FlashVars in the OBJECT tags, you can insert dynamic data into a Flash movie. Here is an example. This is a direct PHP manipulation, no Flash experience is needed.

 

PHP and XML

The most common use of one-way is done indirectly, using PHP as a feed producer. PHP can write XML and RSS formatted files, which can then be displayed within Flash. First, you have to create an XML file. Then once created, anything can read it.

Once an XML is available, Flash can then parse it. Here is how it generally works in Flash. The end result can be very useful.

 

Two-Way Communications

Flash Forms and PHP

The internet is abound with examples of creating Flash forms that do a POST or a GET. Because this is not any different than an normal HTML form, this doesn't tell us much about communications. The key to understanding submitting a form in Flash, and getting a result confirmation or error, is that anything that PHP prints, is available to Flash. In actionscript:

var message:LoadVars = new LoadVars();

message.to = to_input_object.text;

...

We care about what PHP has to do. Let's take this email example:

<?php

// standard POST or GET read, let's say an email

$OK = mail($to, $subject, $message, $additionalheaders);

// let Flash know the result

if ($OK) {

echo "myvarinflash=email successful";

} else {

echo "myvarinflash=Problem with the server, Try again later.";

}

?>

In this example, myvarinflash is typically dynamic text or a Flash variable. From a PHP perspective, it's all about assigning values to Flash variable names. If there are more variables, just echo them, with name=value, no quotes.

Flash Managers and PHP

Flash can be used to manage PHP scripts, by feeding values, and by using timing events via Flash movieclips or standard keyframe actions. In this example, we are going to use a variant of loadVariables, called loadVariablesNum(). We are going to have a Flash movie repeatedly call a PHP script, based on movieclip timings, and an enabling checkbox. Here's a real example of a PHP script, used as a bot, to process database updates.

Now that we have prepared the PHP, let's control a PHP script's execution by using Flash. A single Flash movie can have many movieclips, all managing different PHP scripts independently.

 

Conclusion

There are direct and indirect ways to share information between Flash and PHP. He can use single or dual communications. We can use Flash to create standard form processing. We can use PHP to control Flash data from HTML and XML. We can use PHP to control Flash data, and Flash objects by using LoadVars and loadVariables. We can also use Flash to control if, and how often to call PHP scripts. Now you know what options are available to you as a PHP programmer, when asked to interface with a Flash programmer.