Flashas3 communicates with PHP

Source: Internet
Author: User
Flash 3 communicates with PHP. Currently, flash is widely used in various aspects. flash not only focuses on its own brilliant results, but also needs to exchange data with external programs to achieve more powerful functions, with the arrival of as3, The flash and external interaction methods become more and more convenient and rational. Today, I will talk about the interaction between as3 and backend data (here I select the as3 php architecture, and other configurations are the same ).

In as3, the original loadVars method has been deprecated. Instead, a series of classes are used to interact with background data. These classes are: URLLoader, URLLoaderDataFormat, URLStream, and URLVariables.
Similar to the original loadVars, the URLLoader class is a powerful tool for loading text, binary data, xml, URL data, and other information. URLLoader accepts a URLRequest as the constructed parameter and uses the load method to load data. Urloder can also listen, which is different from loadVars. A simple example is as follows:

1. /////////////// actionscript code //////////////////// //////////////////////
2. // declare a URLLoader
3. var loader: URLLoader = new URLLoader ();
4. // listen to events that have been loaded for several times
5. loader. addEventListener (Event. COMPLETE, loaded );
6. // event-related functions
7. function loaded (e: Event ){
8. trace (loader. data );
9. // output: this data is from php!
10 .}
11. // load test. php
12. loader. load (new URLRequest ("test. php "));

1. /// // test. php code /////////////////////////////////
2. 3. // declare a variable and assign it to a string
4. $ data = 'This data is form php! ';
5. // output this variable to flash
6. echo $ data;
7.?>

From the above example, we can see how simple as3 is to interact with the background, but this is only a single interaction. what should I do if I want flash to send data to the background and return the data? At this time, you may make the code as follows:

1. // actionscript code
2. var loader: URLLoader = new URLLoader ();
3. loader. data = {message: "hello im flash! "};
4. loader. addEventListener (Event. COMPLETE, loaded );
5. function loaded (e: Event ){
6. trace (loader. data );
7. // output: this is flash say:
8 .}
9. loader. load (new URLRequest ("test. php "));

1. // test. php code //
2. 3. $ flashData = $ _ POST ['message'];
4. echo "this is flash say: $ flashData ";
5.?>

Look at the output code. what do you get? Yes, the information is not transmitted to php. you are still trying to transmit data in loadVars mode, but this is not the case in as3, the data of URLLoader is initialized only when the data is downloaded. when the data is not loaded, it is null. that is to say, it only contains the received data, regardless of the data to be sent, how does as3 send data to the background? Use URLVariables. URLVariables allows you to transmit variables between flash and background programs. as3 has separated the sending and receiving data. Unlike as2's loadVars, a class can all be used, then how does this URLVariables send the data to be sent?

You may have searched the URLLoader document and not found anything related to it. Yes, URLLoader is not associated with URLVariables, but URLRequest is using it. URLRequest contains all the information of the entire http request, therefore, the data to be sent should be placed here. URLRequest has a data attribute that accepts an object-type parameter, which is the data to be sent. At the same time, you may also notice how URLRequest manages to send http requests. its method attribute accepts a character parameter, post, or get. Now let's see how they work:

1. // actionscript code
2. var loader: URLLoader = new URLLoader ();
3. // declare a URLRequest
4. var url: URLRequest = new URLRequest ("test. php ");
5. // Set the transmission mode to POST.
6. url. method = URLRequestMethod. POST;
7. // declare a URLVariables
8. var values: URLVariables = new URLVariables ();
9. // Set the information to be transmitted
10. values. message = "hello im flash! ";
11. url. data = values;
12. loader. addEventListener (Event. COMPLETE, loaded );
13. function loaded (e: Event ){
14. trace (loader. data );
15. // output: this is flash say: hello im flash!
16 .}
17. loader. load (url );

1. // test. php code //
2. 3. $ flashData = $ _ POST ['message'];
4. echo "this is flash say: $ flashData ";
5.?>

How about it? is it easy to implement data transmission between as3 and the background? Well, now our program can interact with data in two directions, but this is just some simple data. if you want to transmit structured data, (Anyone familiar with as2 knows that loadVars can automatically parse the structure of the downloaded data.) let's try using loadVars to see if URLLoader can automatically parse the downloaded data.

1. // actionscript code
2. var loader: URLLoader = new URLLoader ();
3. // declare a URLRequest
4. var url: URLRequest = new URLRequest ("test. php ");
5. // Set the transmission mode to POST.
6. url. method = URLRequestMethod. POST;
7. // declare a URLVariables
8. var values: URLVariables = new URLVariables ();
9. // Set the information to be transmitted
10. values. message = "hello im flash! ";
11. url. data = values;
12. loader. addEventListener (Event. COMPLETE, loaded );
13. function loaded (e: Event ){
14. trace (loader. data );
15. // outPut: phpMessage = im php message! & FlashMessage = hello im flash!
16. trace (loader. data. phpMessage );
17. // thorw Error;
18. trace (loader. data. flashMessage );
19. // thorw Error;
20 .}
21. loader. load (url );

1. // test. php code //
2. 3. $ flashData = $ _ POST ['message'];
4. $ phpMessage = 'im php message! ';
5. echo "phpMessage = $ phpMessage & flashMessage = $ flashData ";
6.?>

We can see that the data has been completely downloaded but not parsed, and an exception is thrown. Is it because URLLoader cannot automatically parse the data? No. On the contrary, URLLoader provides multiple data parsing methods for you to choose from. These data parsing methods are all in URLLoaderDataFormat. they are BINARY-parses TEXT in BINARY-parses VARIABLES in TEXT-parses VARIABLES in variable-value pairs. The dataFormat attribute of URLLoader provides a resolution option. let's modify the program to make it more perfect:

1. // actionscript code
2. var loader: URLLoader = new URLLoader ();
3. // declare a URLRequest
4. var url: URLRequest = new URLRequest ("test. php ");
5. // Set the transmission mode to POST.
6. url. method = URLRequestMethod. POST;
7. // declare a URLVariables
8. var values: URLVariables = new URLVariables ();
9. // Set the information to be transmitted
10. values. message = "hello im flash! ";
11. url. data = values;
12. // Set the data to be parsed using variable-value pairs
13. loader. dataFormat
= URLLoaderDataFormat. VARIABLES;
14. loader. addEventListener (Event. COMPLETE, loaded );
15. function loaded (e: Event ){
16. trace (loader. data );
17. // outPut: phpMessage = im php message! & FlashMessage = hello im flash!
18. trace (loader. data. phpMessage );
19. // im php message!
20. trace (loader. data. flashMessage );
21. // hello im flash!
22 .}
23. loader. load (url );

1. // test. php code //
2. 3. $ flashData = $ _ POST ['message'];
4. $ phpMessage = 'im php message! ';
5. echo "phpMessage = $ phpMessage & flashMessage = $ flashData ";
6.?>

The above is the basic content for interaction between as3 and the background. now you can create such interaction in as3 to obtain dynamic data to enrich your program. Before you start your own experiment, there are other things worth noting:
1. remember to capture and handle exceptions (in the above example, it is assumed that they are running in a perfect test environment, so no code is added for exception capture and processing, but in reality, you may encounter various situations.) URLLoader may cause several exceptions (for details, refer to the instructions)
You should listen to ioError and securityError events, or try-catch to handle them, even if you are sure this will not happen.
2. file encoding. this is a common problem. many data may become garbled or parse errors because of encoding problems, my approach is to ensure that both the sent data and the data returned by the background are in UTF-8 format (because flash is encoded in UTF-8 by default)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.