Communication between ActionScript3 and PHP is often used in Flash application development to communicate with the Web server, storing the current application status on the server or obtaining information from the server. Flash has provided support for Http and network Socket. For Http support, Flash uses URLRequest, URLLoader, URLVariables, and other classes to complete operations.
1. obtain external images and other resources in Flash
In ActionScript3, external resources are obtained mainly through the URLRequest and URLLoader classes. you can register the Complete and Progress events of the URLLoader class to monitor and obtain the status of external resources.
var myLoader:URLLoader=new URLLoader();var myXmlLocation:URLRequest = new URLRequest("test.xml");myLoader.load(myXmlLocation);myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,xmlLoadComplete);
If the external resources we obtain are in binary format, we need to use the Loader class to replace the URLLoader class.
2. transfer Flash information to the Web server
In Flash, data must be transmitted to the Web server. we can only use POST or GET. The POST action can transmit more data than GET. the specific difference is that the Http entity body, for POST, the sent value is in the entity body. for GET, the entity body is always empty. For HTML forms, you can specify in Form Method whether the current data is transferred as POST or GET. However, in Flash, you must specify whether the current operation is POST or GET in the URLRequest object. In Flash, URLRequest, URLVariables, and URLLoader are required to transmit data to the Web server.
var values:URLVariables =new URLVariables(); values.key=”Message”; var header:URLRequestHeader = new URLRequestHeader("pragma", "no-cache"); var request:URLRequest=new URLRequest("http://localhost/shopguide/data.php"); request.data=values; request.requestHeaders.push(header); request.method=URLRequestMethod.POST; var loader:URLLoader=new URLLoader(); try { loader.load(request); } catch (error:ArgumentError) { trace("An ArgumentError has occurred."); } catch (error:SecurityError) { trace("A SecurityError has occurred."); }
The above code uses URLVariables to construct the key-value pair for data transmission. in this way, on the PHP end, you can use $ _ POST ["key"] to obtain the transmitted data. In the code, URLRequestHeadere is an Http request header. even if the application has a cached copy of the requested content, the request should be forwarded to the original server. The following figure shows the request information under the developer tool of IE9.
On the PHP server, we only need echo ($ _ POST ['key']); to get the request information, the response body of the third image is the echo output in PHP.
3. use php: // input to obtain the Flash request content without a specified key-value pair
In the script 3.0 programming, an example of passing XML to a Web server does not use the URLVariables class to construct a key-value pair. the sample code is as follows:
var secondsUTC:Number = new Date().time;var dataXML:XML =
{secondsUTC}
Ernie
guru
;var request:URLRequest = new URLRequest("http://localhost/shopguide/data.php");request.contentType = "text/xml";request.data = dataXML.toXMLString();request.method = URLRequestMethod.POST;var loader:URLLoader = new URLLoader();try{ loader.load(request);}catch (error:ArgumentError){ trace("An ArgumentError has occurred.");}catch (error:SecurityError){ trace("A SecurityError has occurred.");}
The difference between the request sent by this code and the URLVariables class is that the request content is directly sent, that is, there is no key value. The request content is as follows:
How can I retrieve data in PHP for a request like this? Later, I found out that it can be obtained through file_get_contents ("PHP: // input") of php. In PHP, file_get_contents returns a string. Output the content of file_get_contents and we can see the following response body.
For details about php: // input, refer to this article:
Php: // What does input mean? Introduction to php input stream input
The Socket class and XMLSocket class are also provided in ActionsScript3. for details about how to use them, refer to the book "ACTIONSCRIPT 3.0 programming.