AS3 Five ways to communicate with PHP (based on HTTP protocol)
/** * This is based on the HTTP protocol * altogether introduced five methods */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 u Rlloader: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 the data inside PHP *//-------- --------------------------Urlloader.dataformat = urlloaderdataformat.variables; /** * URLLoaderDataFormat.BINARY:String----> Specify to receive downloaded data as raw binary data * urlloaderdataform At. text:string----> Specify to receive downloaded data as text * URLLoaderDataFormat.VARIABLES:String----> Specify to receive downloaded data as a URL-encoded variable */Urlloader.load (Phpurl); Urlloader.addeventlistener (Event.complete, completeHandler1); -----------------------------------/** * Second type: Read PHP-generated XML *//-------------------------------------urlloader.load (Phpurl); Urlloader.addeventlistener (Event.complete, CompleteHandler2); //-------------------------------------/** * Third: Pass parameters to PHP inside the Get method */ --------------------------------------Phpurl.method = Urlrequestmethod.get; Phpurl.data = "Data to be outgoing"; Urlloader.load (Phpurl); Urlloader.addeventlistener (Event.complete, CompleteHandler3); --------------------------------------/** * Fourth: Upload parameters to PHP using the Post method *//--------------------------------------Phpurl.method = urlreques Tmethod.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 type: Binary communication method *//----------------------------------------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 value is a custom node value inside 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-already outgoing data:" + (E . Currenttarget as URLLoader). data); } Private Function CompleteHandler4 (e:event): void {trace ("post-has outgoing data:" + ( E.currenttarget as URLLoader). data); } Private Function CompleteHandler5 (e:event): void { var _bytearr:bytearray = e.currenttarget.data as ByteArray; Trace (_bytearr.readint ()); Trace (_bytearr.readbyte ()); Trace (_bytearr.readutf ()); Cnsloppyman}}}
?
?