views:

630

answers:

4

Hi all,

Is it possible to request some data in a Flash movie from PHP at run-time? Maybe my real-world implementation can clarify some things:

I use a Flash movie to store a Local Shared Object (because for some reason I need LSO's instead or regular PHP cookies). Now, when I load up a PHP file I want to somehow retrieve the data from the LSO at runtime, assign it to some variables, and use the variables through the rest of the script.

Doing some research makes me believe it's not possible in the way I intend. So any other suggestions, methods or solutions are highly welcome.

Thanks in advance.

A: 

Use getURL() you can fetch a page that returns the needed data or sets the needed data.

What I would suggest is that you call a page like flashExchange.php via

getURL('flashExchange.php?cacheBust=' + random(99999), '_blank');

You can add more GET variables or you can POST data, with this.

Unkwntech
This would be the case when you are 'viewing' the flash movie, and 'requesting' data from a PHP script. I need it the other way around, when the user is 'viewing' the PHP script and 'requesting' data from a Flash movie. Maybe want things that are not technically possible :)...
SolidSmile
I'd just load an empty flash file with the code to post the data over to the PHP script then set it in the SESSION and forward to another page (or the same page with a var) that uses the data.
Unkwntech
Not too bad... but maybe redirecting is not the greatest solution because the technique will be applied to the frontpage of a traffic heavy site. I want to keep the method as light-weight as possible, the user shouldn't even have to notice it.
SolidSmile
Might be a way to do it with some AJAX
Unkwntech
+1  A: 

There are two ways that we do it here (and we do this a fair bit). One is to write your PHP so that it outputs well-formed XML which can be consumed by your Flash application. The other is using AMFPHP which is a little more complicated to configure, but it can do quite a lot in terms of translating PHP objects/arrays into native Flash objects.

Not sure if this fits your particular situation exactly, but in terms of a general solution for retrieving data from PHP inside a Flash application, it gets the job done.

inkedmn
I'm familiar with both methods, but both work with the Flash movie requesting data from a PHP page. I want to run a PHP script which is requesting data from a Flash movie, if it is at all possible.
SolidSmile
A: 

in my opinion (i'm not a flash coder) ...

you have to push to php (instead of pull from swf), meaning your flash movie has to report its LSO's state if it changes. why? because your server doesn't know the running swf instances.

you can do this by ...

  • either continually post a serialized version of the LSO to the server (and save it to a file or db)

    => if you run your php script it works with the most recent data

    very simple and straight forward. probably the way to go.

  • or create a socket server in your php script. the swf now tries to connect to the server continuously. if the server answers, post the serialized LSO.

    => if you run your php script it blocks until a connection is made and then works with live data

    writing a socket server in php is neither trivial nor hard, but you need to be able to run php in cli mode

problems:

  1. if the flash movie is public, there may be a lot of instances running - you need to tell the clients apart, otherwise you wont know who reports it's LSO.

  2. if there are lots of instances of this flash movie running, you'll be constantly bombarded with requests.

  3. for the socket server solution, you'll need ...

    • to be able to run php as a cli-app
    • an open port in the firewall. 3rd party hosters won't allow this (until you're extremely lucky or unlucky).

there may be other ways i don't know of.

Schnalle
+2  A: 

The best way to intercommunicate Flash and PHP is XML (don't forget to use UTF-8!).

in PHP:

$xml = new DOMDocument('1.0', 'UTF-8');
$doc = $xml->appendChild($xml->createElement('my-root-element'));
...
header('Content-Type: text/xml; charset=utf-8');
echo $xml->saveXML();

In as3

var myLoader:URLLoader = new URLLoader();
var req:URLRequest = new URLRequest('http://host.com/my_xml.php');
myLoader.addEventListener(Event.COMPLETE, onMyXMLLoad);
myLoader.load(req);

function onMyXMLLoad(evt:Event)
{
    trace(evt.target.data);
    var xml:XML = new XML(evt.target.data);
    ...
}

You could also read about ExternalInterface... Yes, sometimes it helps... You may want to generate dynamicaly your JS to communicate with flash movie.

Jet
I think ExternalInterface is the way to go in my case. Although through JS, this is exactly what I need to pull some data from a Flash movie and act accordingly. Because it's not a critical functionality, I can afford visitors not supporting JS. Thanks for leading me in the right direction.
SolidSmile
u r welcome ) hope, you don't need a copy-paste solution?
Jet