Passing Object from Flash AS3 to PHP
In previous post, we learnt how to pass an JSON encoded object from PHP to AS3. How about passing an Object from AS3 to PHP?
This example will show you how to do it. Take a look at the Flash ActionScript.
import flash.net.URLRequest; import flash.net.URLLoader; import com.adobe.serialization.json.JSON; var people:Array = new Array(); var person:Object = new Object(); person.firstname = "Kobe"; person.lastname = "Bryant"; people.push(person); var url:String = "http://localhost/getJSON.php"; var request:URLRequest = new URLRequest(url); request.method = URLRequestMethod.POST; var requestVars:URLVariables = new URLVariables(); requestVars.myObject = JSON.encode(people); request.data = requestVars; var loader:URLLoader = new URLLoader(); loader.load(request);
The script above encode the Object people with JSON, and pass it to variable name "myObject". "myObject" will be passed to PHP script using POST method.
Next we will take a look at the PHP script, getJSON.php
$object = JSON_decode($_POST['myObject']);
$fp = fopen('data.txt', 'w');
fwrite($fp, $_POST['abc']);
fwrite($fp, $object[0]->firstname . " ");
fwrite($fp, $object[0]->lastname . chr(13) . chr(10));
fwrite($fp, $_POST['myObject']);
fclose($fp);
The script decode the JSON Object, $_POST['myObject'] passed from the AS3. Then it output the data to data.txt with the following output:
Kobe Bryant
[{"firstname":"Kobe","lastname":"Bryant"}]
The 2nd line is the encoded JSON object which is not decoded yet.
Remember that ascorelib is needed for JSON encoding & decoding in ActionScript. Extract the ascorelib zip file, locate the src\com folder and copy it to your Flash CS4 project folder.
Example files is provided. Download here.

Like
December 17th, 2009 - 09:05
How can you tell that this succeeded? Can you listen for it to echo ‘success’ or something from php to flash?
December 17th, 2009 - 10:26
Hi Jeff, of course you can return the value from php to flash. I output the values to the text file to indicate that the variables were passed from Flash to PHP.
Visit: http://digitalmemo.neobie.net/2009/05/04/passing-object-from-php-to-flash-using-json/
February 5th, 2010 - 07:10
True but this example seems different as you are doing a POST. It would be nice to be able to ‘listen’ for a success or failure code. Thanks!