AS3 Five ways to communicate with PHP (based on HTTP protocol)

Source: Internet
Author: User
Tags vars
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}}}
?

?

  • Related Article

    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.