Five methods for AS3 to communicate with PHP (based on HTTP protocol)/*** This is based on HTTP protocol * A total of five methods */package {importflash. display. five methods for Sprite; AS3 to communicate with PHP (based on HTTP protocol)
/*** This is based on HTTP * A total of five methods are introduced */package {import flash. display. sprite; import flash. events. *; import flash.net. URLLoader; import flash.net. URLRequest; import flash.net. URLVariables; import flash.net. URLRequestMethod; import flash.net. URLLoaderDataFormat; import flash.net. URLRequestHeader; import flash. utils. byteArray ;/***... * @ author ..... michael zhang * @ contact... QQ: 271291823 */public class ASWithPHP_1 extends Sprite {private var urlLoader: URLLoader; private var phpUrl: URLRequest; public function ASWithPHP_1 () {init ();} private function init (): void {urlLoader = new URLLoader (); phpUrl = new URLRequest ("PHP address");/*** first: directly read data in PHP * // ------------------------------------ urlLoader. dataFormat = URLLoaderDataFormat. VARIABLES;/*** URLLoaderDataFormat. BINARY: String ----> specify to receive downloaded data in the form of raw BINARY data * URLLoaderDataFormat. TEXT: String ----> specify to receive downloaded data in TEXT format * URLLoaderDataFormat. VARIABLES: String ----> specify to receive downloaded data in the form of URL encoded VARIABLES */urlLoader. load (phpUrl); urlLoader. addEventListener (Event. COMPLETE, completeHandler1); // -----------------------------------/*** Type 2: Read the xml generated by PHP * // ------------------------------------------- urlLoader. load (phpUrl); urlLoader. addEventListener (Event. COMPLETE, completeHandler2); // -------------------------------------/*** method 3: Pass the parameter to PHP through the GET method * // ------------------------------------ phpUrl. method = URLRequestMethod. GET; phpUrl. data = "data to be transmitted"; urlLoader. load (phpUrl); urlLoader. addEventListener (Event. COMPLETE, completeHandler3); // --------------------------------------/*** method 4: Pass the parameter to PHP through the POST method * // ------------------------------------ phpUrl. method = URLRequestMethod. POST; var vars: URLVariables = new URLVariables (); vars. value1 = "parameter 1"; vars. value2 = "parameter 2"; phpUrl. data = vars; urlLoader. dataFormat = URLLoaderDataFormat. VARIABLES; urlLoader. load (phpUrl); urlLoader. addEventListener (Event. COMPLETE, completeHandler4); // --------------------------------------/*** fifth: binary communication method * // callback var requestHeader: URLRequestHeader = new URLRequestHeader ("Content-type ", "application/octet-stream"); phpUrl. method = URLRequestMethod. POST; phpUrl. requestHeaders. push (requestHeader); var byteArr: ByteArray = new ByteArray (); byteArr. writeByte (12); byteArr. writeUTF ("CNSloppyMan"); var sendData: ByteArray = new ByteArray (); sendData. writeInt (byteArr. length); sendData. writeBytes (byteArr); phpUrl. data = sendData; urlLoader. dataFormat = URLLoaderDataFormat. BINARY; urlLoader. load (phpUrl); urlLoader. addEventListener (Event. COMPLETE, completeHandler5); // -----------------------------------------} private function completeHandler1 (e: Event): void {var vars: URLVariables = URLVariables (e. currentTarget as URLLoader ). data); trace ("accept data:" + vars. value); // assume that the value is the custom node value in PHP} private function completeHandler2 (e: Event): void {var xml: XML = new XML (e. currentTarget as URLLoader ). data); trace ("xml data:" + xml. toString ();} private function completeHandler3 (e: Event): void {trace ("GET-outgoing data:" + (e. currentTarget as URLLoader ). data);} private function completeHandler4 (e: Event): void {trace ("POST-outgoing data:" + (e. currentTarget as URLLoader ). data);} private function completeHandler5 (e: Event): void {var _ byteArr: ByteArray = e. currentTarget. data as ByteArray; trace (_ byteArr. readInt (); // 14 trace (_ byteArr. readByte (); // 12 trace (_ byteArr. readUTF (); // CNSloppyMan }}}
?
?