dcsimg
A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: PHP AS3 communication

  1. #1
    Member
    Join Date
    Aug 2014
    Posts
    46

    PHP AS3 communication

    Hi all !

    I have been wracking my brains over this seemingly simple code & issue but cannot get it to work. The code snippet, invokes a php file using URLLoader & passing 2 variables to php. The simple php file grabs a swf file, base64 encodes it and sends it back to AS3.

    Code:
    stop();
    var rs:String ="";
    var len:int = 0;
    var bytes:ByteArray = new ByteArray();
    
    var variables:URLVariables = new URLVariables();	
    var varSend:URLRequest = new URLRequest("comm_1.php");
    varSend.method = URLRequestMethod.POST;
    varSend.data = variables;
    var varLoader:URLLoader = new URLLoader;
    varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
    variables.uname = "Guest";
    variables.lname = "Newbie";
    variables.sendRequest = "parse";
    varLoader.load(varSend);
    varLoader.addEventListener(Event.COMPLETE,completeHandler);
    
    function completeHandler(event:Event):void
    {
       // SECTION 1
       //==========
        rs = event.target.data.returnString;
       len = rs.length;
    	msg1.text = rs+' - '+len;
    		
    //	rs = "Q1dTCHAKAAB4nIVWa1RTVxbe94YbEl4CJiYg0KDCRFAQBCEd0PAQRR7lrTgCCRAExARDRLCd8WpRsSKFAlGstoro8BRGRNEZhCAJz64C4ixdylQs2hG1ZawL7VIncy6U9sqfuWvlZp/v7L3vt7+z9lk7D4gkAHY3gAkGgRaAngBcr9evY+iRWRgEhhCgyFIoN6fLZFlJO1MgR6XIBv/dKpVCTi0V8gilLCcHklJ2K5UyuSpNKd0pg+0KlcJPnhpN+WZnSfNBbQxM+PVhgY0aBxYWxXsPxX9D8bkFo5hFbeHLQQm/owbhagIMMbQRbHkaBxdQm4EhjpYzmAHGIuZlnv0eM4pXicPyGW/s/3ojHh+BnvMXhGCQ6ubz0s1nKnWNT/UanzMYZn7tkWE5UgZJpHfS60GfgEz9PkC61SFIP6qnTGoX46AEg8iw8Pysrf60l71NWCcWeh94SyailS5i+LKcZLcVu6wXPpxwF2Mhy4A1bhNVbNEJqeUk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Ha/R9yOWqh";
    
    
        // SECTION 2
    // =========
    
    	var decoder:Base64Decoder = new Base64Decoder();
    	decoder.decode(rs);
    	var bytes:ByteArray = decoder.toByteArray();
    	trace(bytes);
    	trace(len);
    
    	
    }
    IF I comment out section 2 of the code ( the decoding part), I can see that the string is passed just fine and displayed in a text box by
    Code:
    msg1.text = rs+' - '+len;
    All well till here. I have pasted the actual value of string, in commented out rs string, in the code for comparison/checking purpose.

    So the value of rs received from php and the value in commented out rs string are same.

    Now if I un-comment section 2, which tries to base64 decode the string back, I immediately get the following error:

    Error
    at Base64Decoder/flush()
    at Base64Decoder/toByteArray()
    at AS3_swf_php_comm_1_fla::MainTimeline/completeHandler()
    at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunctio n()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/flash.net:URLLoader:nComplete()
    The php code is as below:

    PHP Code:
    if($_POST[‘sendRequest’] ===“parse”){

    var_dump($_POST);

    $uname $_POST[‘uname’];
    $lname $_POST[‘lname’];

    $p file_get_contents(‘dummy.swf’);
    $p base64_encode($p);
    echo 
    &returnString=.$p;
    exit;

    I have been trying to figure this out for a long time without success. Please someone enlighten me what am I doing wrong and what is it that I need to do to get this working.

    Thanks to all !!

  2. #2
    Senior Member Alloy Bacon's Avatar
    Join Date
    Apr 2011
    Posts
    941
    I use this script for as2/as3 to encode/decode directly in flash so no php involved, you call the last 2 functions in this script to encode or decode the string:

    PHP Code:
    function Encode(str){
    return 
    encodeBase64(str);
    }
    function 
    Decode(str){
    trace(decodeBase64(str));
    }



    function 
    StringReplaceAll(sourcefindreplacement){
        
    return 
    source.split(find).join(replacement);
    }
    var 
    _EndOfInput = -1;
    var 
    _Chars:Array = new Array(
    'A','B','C','D','E','F','G','H',
    'I','J','K','L','M','N','O','P',
    'Q','R','S','T','U','V','W','X',
    'Y','Z','a','b','c','d','e','f',
    'g','h','i','j','k','l','m','n',
    'o','p','q','r','s','t','u','v',
    'w','x','y','z','0','1','2','3',
    '4','5','6','7','8','9','+','/'
    );
    var 
    _CharsReverseLookup:Array; // = new Array();
    var _CharsReverseLookupInited:Boolean InitReverseChars();
    var 
    _Digits = new Array( '0''1''2''3''4''5''6''7''8''9''a''b''c''d''e''f' );

    var 
    _base64Str:String;
    var 
    _base64Count:Number;

    function 
    InitReverseChars(){
    _CharsReverseLookup = new Array();
    for (var 
    i=0_Chars.length;i++){
    _CharsReverseLookup_Chars[i] ] = i;
    }
    return 
    true;
    }

    function 
    UrlDecode(str){
    str StringReplaceAllstr"\+"" " );
    str unescapestr );
    return 
    str;
    }

    function 
    UrlEncode(str){
    str escapestr );
    str StringReplaceAllstr"\+""%2B" );
    str StringReplaceAllstr"%20""+" );
    return 
    str;
    }

    function 
    setBase64Str(str){
    _base64Str str;
    _base64Count 0;
    }

    function 
    readBase64(){
    if(!
    _base64Str){
    return 
    _EndOfInput;
    }

    if(
    _base64Count >= _base64Str.length){
    return 
    _EndOfInput;
    }

    var 
    c:Number _base64Str.charCodeAt(_base64Count) & 0xff;
    _base64Count++;
    return 
    c;
    }

    function 
    encodeBase64(str){
    setBase64Str(str);
    var 
    result "";
    var 
    inBuffer = new Array(3);
    var 
    lineCount 0;
    var 
    done false;

    while(!
    done && (inBuffer[0] = readBase64() ) != _EndOfInput ){
        
    trace(done);
    inBuffer[1] = readBase64();
    inBuffer[2] = readBase64();

    result += ( _CharsinBuffer[0] >> ]);

    if( 
    inBuffer[1] != _EndOfInput ){
    result += ( _Chars[ ( ( inBuffer[0] << 4) & 0x30) | (inBuffer[1] >> 4) ] );
    if( 
    inBuffer] != _EndOfInput ){
    result += ( _Chars[ ( ( inBuffer] << 2) & 0x3c) | ( inBuffer] >> 6)] );
    result += ( _CharsinBuffer] & 0x3F ]);
    }else{
    result += ( _Chars[((inBuffer] << 2) & 0x3c)]);
    result += ("=");
    done true;
    }
    }else{
    result += ( _Chars[ ( ( inBuffer[0] << ) & 0x30 ) ] );
    result += "=";
    result += "=";
    done true;
    }
    trace(result)
    lineCount += 4;

    if(
    lineCount >= 76){
    result += ('\n');
    lineCount 0;
    }
    }
    return 
    result;
    }

    function 
    readReverseBase64(){
    if(!
    _base64Str){
    return 
    _EndOfInput
    }

    while(
    true){
    if( 
    _base64Count >= _base64Str.length ){
    return 
    _EndOfInput
    }

    var 
    nextCharacter:String _base64Str.charAt(_base64Count);
    _base64Count++;

    if(
    _CharsReverseLookup[nextCharacter]){
    return 
    _CharsReverseLookup[nextCharacter];
    }

    if(
    nextCharacter == 'A'){ 
    return 
    0
    }
    }
    return 
    _EndOfInput;
    }

    function 
    ntos(n){
    var 
    str:String n.toString(16);

    if(
    str.length == 1str "0" str;
    str "%" str;
    return 
    unescape(str);
    }

    function 
    decodeBase64(str){
    setBase64Str(str);
    var 
    result:String "";
    var 
    inBuffer:Array = new Array(4);
    var 
    done:Boolean false;

    while(!
    done && (inBuffer[0] = readReverseBase64()) != _EndOfInput && ( inBuffer[1] = readReverseBase64()) != _EndOfInput){
    inBuffer] = readReverseBase64();
    inBuffer] = readReverseBase64();
    result += ntos( ( ( ( inBuffer] << ) & 0xff ) | inBuffer] >> ) );

    if( 
    inBuffer] != _EndOfInput ){
    result += ntos( ( ( ( inBuffer] << ) & 0xff ) | inBuffer] >> ) );
    if(
    inBuffer[3] != _EndOfInput){
    result +=  ntos((((inBuffer[2] << 6)  & 0xff) | inBuffer[3]));
    }else{
    done true;
    }
    }else{
    done true;
    }
    }
    return 
    result;
    }

    function 
    toHex(n){
    var 
    result:String "";
    var 
    start true;
    for( var 
    i:Number=32i>0;){
    i-=4;
    var 
    digit = (n>>i) & 0xf;
    if(!
    start || digit != 0){
    start false;
    result += _Digits[digit];
    }
    }
    return ( 
    result=="" "0" result );
    }

    function 
    pad(strlenpad){
    var 
    result:String str;
    for (var 
    i=str.lengthi<leni++){
    result pad result;
    }
    return 
    result;
    }

    function 
    encodeHex(str){
    var 
    result:String "";
    for( var 
    i:Number=0i<str.lengthi++){
    result += padtoHexstr.charCodeAt) & 0xff ), 2'0');
    }
    return 
    result;
    }

    function 
    decodeHex(str){
    var 
    result "";
    var 
    nextchar "";

    for( var 
    i=0i<str.lengthi++ ){
    nextchar += str.charAt(i);

    if(
    nextchar.length == 2){
    result += ntosparseInt("0x" nextchar));
    nextchar "";
    }
    }
    return 
    result;
    }

    trace(encodeBase64("hey"))
    trace(decodeBase64("aGV5")) 
    uber is a chupacabra

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center