Five methods for as3 to communicate with PHP (based on HTTP Protocol)

Source: Internet
Author: User

Based on the HTTP protocol, and then mainly the following five methods.

 

 

1/** 2 * This is HTTP-based 3 *. Five methods are introduced in total: 4 */5 6 Package 7 {8 Import flash. display. sprite; 9 Import flash. events. *; 10 Import flash.net. urlloader; 11 import flash.net. URLRequest; 12 Import flash.net. urlvariables; 13 Import flash.net. urlrequestmethod; 14 Import flash.net. urlloaderdataformat; 15 Import flash.net. urlrequestheader; 16 Import flash. utils. bytearray; 17/** 18 *... 19*20*21 */22 23 public class aswithphp_1 extends sprite 24 {25 private var urlloader: urlloader; 26 private var phpurl: URLRequest; 27 28 public function aswithphp_1 () 29 {30 Init (); 31} 32 33 private function Init (): void 34 {35 urlloader = new urlloader (); 36 phpurl = new URLRequest ("php address "); 37 38/** 39 * First: directly read data in PHP 40 */41 // -------------------------------------------------------------------------- 42 urlloader. dataformat = urlloaderdataformat. variables; 43/** 44 * urlloaderdataformat. binary: String ----> specify to receive downloaded data in the form of raw binary data 45 * urlloaderdataformat. text: String ----> specify to receive downloaded data in text format 46 * urlloaderdataformat. variables: String ----> specify to receive downloaded data in the form of URL encoded variables 47 */48 urlloader. load (phpurl); 49 urlloader. addeventlistener (event. complete, completehandler1); 50 // Route 51 52/** 53 * Second: Read XML 54 */55 // route 56 urlloader generated by PHP. load (phpurl); 57 urlloader. addeventlistener (event. complete, completehandler2); 58 // 59 60/** 61*3: Pass the parameter to PhP using the get method 62 */63/64 phpurl. method = urlrequestmethod. get; 65 phpurl. data = "data to be transmitted"; 66 urlloader. load (phpurl); 67 urlloader. addeventlistener (event. complete, completehandler3); 68 // pushed 69 70/** 71 * Fourth: Pass the parameter to 72 */73/pushed 74 phpurl in PHP using the POST method. method = urlrequestmethod. post; 75 var vars: urlvariables = new urlvariables (); 76 vars. value1 = "parameter 1"; 77 vars. value2 = "parameter 2"; 78 phpurl. data = vars; 79 urlloader. dataformat = urlloaderdataformat. variables; 80 urlloader. load (phpurl); 81 urlloader. addeventlistener (event. complete, completehandler4); 82 // route 83 84/** 85 * Fifth: Binary communication method 86 */87 // route 88 var requestheader: urlrequestheader = new urlrequestheader ("Content-Type", "application/octet-stream"); 89 phpurl. method = urlrequestmethod. post; 90 phpurl. requestheaders. push (requestheader); 91 var bytearr: bytearray = new bytearray (); 92 bytearr. writebyte (12); 93 bytearr. writeutf ("cnsloppyman"); 94 var senddata: bytearray = new bytearray (); 95 senddata. writeint (bytearr. length); 96 senddata. writebytes (bytearr); 97 phpurl. data = senddata; 98 urlloader. dataformat = urlloaderdataformat. binary; 99 urlloader. load (phpurl); 100 urlloader. addeventlistener (event. complete, completehandler5); 101 // parts 102} 103 private function completehandler1 (E: Event): void 104 {105 var vars: urlvariables = urlvariables (E. currenttarget as urlloader ). data); 107 trace ("Accept data:" + vars. value); // assume that the value is the custom node value 108} 109 110 private function completehandler2 (E: Event): void {111 var XML: xml = New XML (E. currenttarget as urlloader ). data); 112 trace ("XML data:" + XML. tostring (); 113} 114 115 private function completehandler3 (E: Event): void {116 trace ("get-Outgoing data:" + (E. currenttarget as urlloader ). data); 117} 118 119 private function completehandler4 (E: Event): void {120 trace ("post-Outgoing data:" + (E. currenttarget as urlloader ). data); 121} 122 123 private function completehandler5 (E: Event): void {124 VaR _ bytearr: bytearray = E. currenttarget. data as bytearray; 125 trace (_ bytearr. readint (); // 14126 trace (_ bytearr. readbyte (); // 12127 trace (_ bytearr. readutf (); // cnsloppyman128} 129} 130 131}
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.