Home >Backend Development >PHP Tutorial >Five ways to communicate between AS3 and PHP (based on HTTP protocol)_PHP tutorial

Five ways to communicate between AS3 and PHP (based on HTTP protocol)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:47:361028browse

First, it is based on HTTP protocol, and then mainly the following five methods.

/**

* This is based on HTTP protocol

* 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 .....Li灬Star

     * @contact...QQ:168527720

    */

       

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");

                                                                

              /**

* The first type: directly read the data in PHP

                               */ 

//------------------------------------------------ -----------------------

​​​​​​ urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;

              /**

* URLLoaderDataFormat.BINARY:String---->Specifies to receive downloaded data in the form of raw binary data

* URLLoaderDataFormat.TEXT:String----> Specifies to receive downloaded data in text form

* URLLoaderDataFormat.VARIABLES:String---->Specifies to receive downloaded data in the form of URL-encoded variables

                               */ 

   urlLoader.load(phpUrl);

​​​​​​ urlLoader.addEventListener(Event.COMPLETE, completeHandler1);

//------------------------------------------------ -----------------------

                                                                

             /**

* The second method: read the xml generated by PHP

                               */ 

//------------------------------------------------ --------------------------

   urlLoader.load(phpUrl);

​​​​​​ urlLoader.addEventListener(Event.COMPLETE, completeHandler2);

//------------------------------------------------ --------------------------

                                                                   

             /**

                  * The third method: pass the parameters to PHP through the GET method

                               */ 

//------------------------------------------------ --------------------------

​​​​​​ phpUrl.method = URLRequestMethod.GET;

​​​​​​ phpUrl.data = "Data to be transferred";

   urlLoader.load(phpUrl);

​​​​​​ urlLoader.addEventListener(Event.COMPLETE, completeHandler3);

//------------------------------------------------ --------------------------

                                                                

              /**

                   * The fourth method: pass the parameters to PHP through the POST method www.2cto.com

                               */ 

//------------------------------------------------ --------------------------

​​​​​​ 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);

//------------------------------------------------ --------------------------

                                                                

              /**

* The fifth method: 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 value is a 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-The data that has been sent out:" + (e.currentTarget as URLLoader).data);                                     

         } 

                                                       

         private function completeHandler4(e:Event):void {

Trace ("Post-data that has been passed out:" + (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

         } 

}  

}

Excerpted from Li Minxin’s column

http://www.bkjia.com/PHPjc/478494.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/478494.htmlTechArticleFirst, this is based on the HTTP protocol, and then mainly the following five methods. /*** This is based on HTTP protocol * A total of five methods are introduced*/ package { import flash.display.Sprite;...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn