Digital Memo All begin with 0 & 1

22Oct/093

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.

5Jul/090

Communication between Flash & Javascript

Sometimes there is a need for communication between Flash & Javascript. Below is the sample codes that work for ActionScript 3.0 and Javascript. The code might work for AS2 as well :)

To call a Javascript function from Actionscript,

function clickSend(eventObj:Object) {
jsArgument = "something";
var resultFromJS:Object=ExternalInterface.call("getTextFromFlash",jsArgument); call getTextFromFlash in Javascript;
received_ti.text; // return the text from JS
}
function getTextFromFlash(str) {
document.htmlForm.receivedField.value = "From Flash: " + str; //str is the value passed from as3 'jsArgument'
return str + " received"; //return 'something received' back to as3
}

Another example is calling ActionScript function from Javascript:

function getTextFromJavaScript(str:String) {
received_ti.text="From JavaScript: "+str;
}
ExternalInterface.addCallback("sendTextToFlash", this, getTextFromJavaScript); //register 'sendTextToFlash()' from javascript to 'getTextFromJavascript()' in ActionScript.

On the Javascript:

function getFlashMovie(movieName) { // return the flash movie as an object
var isIE = navigator.appName.indexOf("Microsoft") != -1;
return (isIE) ? window[movieName] : document[movieName];
}

function formSend() {
var text = document.htmlForm.sendField.value;
getFlashMovie("ExternalInterfaceExample").sendTextToFlash(text); //call sendTextToFlash() with the text as registered earlier in ActionScript
}

You might need to refer Adobe Documentation for more information. For Actionscript 2.0, refer here.

The thumb rule would be:

import flash.external.ExternalInterface;
ExternalInterface.call();
ExternalInterface.addCallback();

Download the sample file. ExternalInterfaceExample.zip