Welcome to Dream.In.Code
Getting Help is Easy!

Join 107,420 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,156 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Pass complex data from PHP to Flash

 
Reply to this topicStart new topic

> Pass complex data from PHP to Flash, Flash MX 2004, PHP, SerializerClass - #1

Broly
Group Icon



post 17 Dec, 2004 - 04:27 PM
Post #1


1 ) Prefaction

In this tutorial we’ll see how to pass complex data from php to flash, without using AmfPhp but also without the “classic” format readable by flash…this can bea n intermediate solution: AmfPhp is for large amount of data, and the classic “var=value&var2=value2&…” is for small/simple data-types…what can we do when we have a little amount of complex data, or a medium amount of simple/complex data ? Well…we can use the serializer class!

1b) What we need

a) Serializer Class (downloadable at [URL=http://sourceforge.net/projects/serializerclass/ )]http://sourceforge.net/projects/serializerclass/)[/URL] – An excellent class, a Sephiroth’s ( http://www.sephiroth.it ) idea that became an useful realty biggrin.gif
As said before, this class allows to pass complex dates from php to flash without the “classic” format

Var=value&var2=value2….

that with a medium amont of data can be very very boring and hard to use, but it allows to pass directly more complex objects (eg Arrays)
This is the official class description

“Serializer Class enable (For Macromedia Flash) you to pass and receive complex data type from and to PHP and Flash using the native PHP functions serialize and unserialize. Flash received data will be a native Flash object”

cool.gif Flash mx 2004 (I think you know it biggrin.gif, if you haven’t it you can find a trial on www.macromedia.com)

c) A local webserver, or an hosting, where test our little example (it must have PHP support)

d) Macromedia Extension Manager (free, downloadable from www.macromedia.com), wich we’ll use for install the Serializer Class Extension

1c) Last notes before start

In this tutorial, we see simply how the data-passage works, we won’t use database, .txt files and other, but only a .php files with “fixed” data….obviously you can take data wherever you want, but I thought that use a data-source (eg a database) in this tutorial hadn’t a practice utility, it’d only make the tutorial more complex and longer…I preferred made things simple smile.gif…it will your work make it harder biggrin.gif


2) Let’s go!

Well, before start, I created the files (serializer.fla and serializer .php). I’ve also downloaded the .mxp file.

NOTE: Obviously you can also create .fla and .php during the tutorial, and not immediately…I preferred create now the files for show how they’ll be biggrin.gif



2a) Serializer Class installation

Now, first thing to do is install the Serializer class.
Double click on .mxp file … follow the video instructions you find at the URL
.


As you see, after the installation, you’ll have a “Serializer” name into the Extension Manager (ehm..I’ve a lot of extension installed now, but if you have less caos then me you found it immediately biggrin.gif). Selecting the Serializer into the Extension Manager you can see a little sample-ActionScript code. You can see with your eyes, it’s very easy. Now I’ll explain you pass-by-pass how to try if the class works correctly, with a small example.

2b) The PHP file

For class testing, we need a php file that have to pass to flash some complex data (array, objects, etc). Let’s write, into “Serializer.php”, some simple code-lines with an array containing different object types…

CODE

<?
$array = array();
$array[0] = true;
$array[1] = "Hello world";
$array["number"] = 1;

// Pass $array to flash

echo "ToFlash=".urlencode(utf8_encode(serialize($array)));


?>


Now, we can see if it really pass data urlencoded and serialized…then we can put the .php on our webspace (or into our local test directory) and visit it in a brower.

The browser’d show this

ToFlash=a%3A3%3A%7Bi%3A0%3Bb%3A1%3Bi%3A1%3Bs%3A11%3A%22Hello+world%22%3Bs%3A6%3A%22number%22%3Bi%3A1%3B%7D

(http://www.brolyweb.com/Tutorials/Serializer/Serializer.php)


2c) The flash side

After the PHP code, it’s time to open Flash 
Create Serializer.fla (or open it), and in the first frame write this code

CODE

import it.sephiroth.Serializer

var serial:Serializer = new Serializer();
// import from PHP
lV = new LoadVars();
lV.onLoad = function(){
unserialized = serial.unserialize(this.ToFlash);
}
lV.load('http://www.brolyweb.com/Tutorials/Serializer/Serializer.php');


(change the path into lV.load to your. Php file)

See the video at URL.



As you see, after inserted the code, if you test your flash movie, you’ll have a variable that have the same values that where in the PHP array…simple, true? smile.gif

3) Bye…and a preview 

In this tutorial we saw how to receive serialized data from php…but if we’d do the vice-versa, send serialized data from flash to php? Well, it’s that we’ll see in next tutorial, friends 
I hope you like this tutorials with video inserts that show some passages wink2.gif, tell me if you like them!!!
(please note: I haven't insert the .swf videos into the thread because they were too large, if I publish it at less than that size they became incomprensible smile.gif)

This post has been edited by Broly: 17 Dec, 2004 - 05:15 PM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

skyhawk133
Group Icon



post 17 Dec, 2004 - 05:32 PM
Post #2
Very cool, love the flash videos!!!
Go to the top of the page
+Quote Post

mullwaden
*



post 18 Nov, 2005 - 05:38 PM
Post #3
uhh now that i have som numbers in some serialize thing, ofcourse risking sounding very stupid, how do i use them, say put them in an array in flash =) I have an array called nums how do i get the numbers into it ? tongue.gif

//cheers

p.s. while im at it, how do i see the amount of values in an array, is it something like array'last ? (thats the ADA version) :S
Go to the top of the page
+Quote Post

nexx
*



post 1 Dec, 2005 - 02:34 PM
Post #4
The script does not work for me, I must admit I'm new to action script.

I'm sure on PHP side everything works ok
in AS it gives me error

Error opening URL "localhost/images/pics/undefined"

I'm passing array to url

CODE

import it.sephiroth.Serializer

var serial:Serializer = new Serializer();
// import from PHP
lV = new LoadVars();
lV.onLoad = function(){
unserialized = serial.unserialize(this.ToFlash);
}

lV.load('http://localhost/test3.php');

///////////////////
this.pathToPics = "images/pics/";
// fill array with pics
this.pArray = unserialized; // if you just declare this here as an array it
// it works fine e.g. ["image0.jpg", "image1.jpg", "image2.jpg", "image3.jpg"];


Could anyone help pls

This post has been edited by nexx: 1 Dec, 2005 - 02:36 PM
Go to the top of the page
+Quote Post

ercherry
*



post 12 Dec, 2005 - 12:53 PM
Post #5
I found this to be very helpful. For the person looking to put the data back into an array once you get it into flash, go to the source:

http://www.sephiroth.it/test/unserializer/

Scroll to the bottom and check out the code.

I'm still figuring it out myself.
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 8/28/08 08:04PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month