Tamagochi
Tamagochi

Reputation:

How can Flash get a variable from a PHP file?

I'm trying to do a PHP-Flash communication in ActionScript 3. I can send from Flash to PHP but I don't know how Flash can get a variable from PHP file.

Any help?

Upvotes: 1

Views: 744

Answers (2)

Antoine Gersant
Antoine Gersant

Reputation: 536

Assuming your PHP code is on a server that has PHP enabled, you could do this :

  • Instantiate an URLRequest object (towards your PHP page) in Actionscript 3
  • Instantiate an URLLoader object and make it load your URLRequest
  • On completion, retrieve your data from the "data" property of the URLLoader object

Sample code (not tested, might be incorrect AS3 because this is Haxe) :

var l : URLLoader = new URLLoader();
var rq : URLRequest = new URLRequest("http://www.example.com/superdata.php");
l.dataFormat = URLLoaderDataFormat.TEXT;
l.addEventListener(Event.COMPLETE, success, false, 0, false);
l.load(rq);

function success (e : Event) : Void {
    trace(e.target.data);
}

And on the PHP side of things :

<? echo "my data"; ?>

Upvotes: 3

Adrian Pirvulescu
Adrian Pirvulescu

Reputation: 4340

You should use AMFPHP and create a webservice. This way you can have SharedObjects and pass variables back and forth.

Upvotes: 1

Related Questions