Sending data from flash to php using post method as2 - actionscript-3

I am using following code in as3:
var Request:URLRequest = new URLRequest();
var xmlMain:XML=new XML();
var urlXmlLoader:Loader = new Loader();
var myVars:URLVariables = new URLVariables();
myVars.RequestXml = xmlMain;
Request.url = "http://www.xyz.php"; //some php url
Request.method = URLRequestMethod.POST;
Request.data = myVars;
urlXmlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlXmlLoader.addEventListener(Event.COMPLETE, getData);
urlXmlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
urlXmlLoader.load(Request);
Now I have an as2 project where I need to do the same. How can it be done in as2??

This is probably the best tutorial on HTTP requests in AS2: http://www.sephiroth.it/tutorials/flashPHP/loadVars/

Related

Sending script to php file with URLLoader returns 0 when I try to GET it

I have a game, I want to write score in my database when the game is over. This is the urlloader function:
public function gameFinished(){
var urlLoader:URLLoader = new URLLoader();
var req:URLRequest = new URLRequest("/../../sendScoreUnit4.php");
var requestVars:URLVariables = new URLVariables();
requestVars.score = total_score_hard;
req.data = requestVars.score;
req.method = URLRequestMethod.POST;
urlLoader.load(req);
}
My sendScoreUnit4.php file tries to get the score like so :
$score = (int)$_GET['score'];
but when I echo it I get 0 every time.
Any help would be appreciated.

Convert Actionscript 3 to Actionscript 2

I have an problem to convert AS3 to AS2
Here is my AS3 code
submit.addEventListener("mouseDown", sendData);
function sendData(evt:Event)
{
//for those using PHP
var students:URLRequest = new URLRequest("http://localhost/phpflash/new_student.php");
students.method = URLRequestMethod.POST;
var posts:URLVariables = new URLVariables();
posts.Fullname = Fullname.text;
students.data = posts;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(students);
}
Like #VC.One said, you must learn AS3, AS2 was dropped and actually Flash CC does not support it.
I do not code in AS2 since many, many years. So this code could contain some errors:
submit.onMouseDown = sendData;
function sendData():Void
{
var vars:LoadVars = new LoadVars();
vars.Fullname = Fullname.text;
var target:Object = new Object();
target.onLoad = function(){
trace("vars sended");
}
vars.sendAndLoad("http://localhost/phpflash/new_student.php", target, "POST");
}

Cancel a POST request

I have an app that makes a request to a server to generate an audio file and the problem that I have is that if I send two distinct and consecutive requests, I can not cancel the first. I tried with .unload or .unloadAndStop but always gives me error.
Any suggestions?
elfich="sonido" + elfichnum;
var request:URLRequest = new URLRequest();
var variables:URLVariables = new URLVariables();
var loader:URLLoader = new URLLoader();
variables.text=caja_texto;
variables.fileName=elfich;
request.url = "http://192.168.0.19:800/cgi-bin/tts.cgi";
request.method = URLRequestMethod.POST;
request.data = variables;
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, cargacompletanew);
loader.addEventListener(IOErrorEvent.IO_ERROR, onErrornew);
loader.load(request);
}
function cargacompletanew(event:Event):void
{
xlocalSound = new Sound();
var xreq:URLRequest = new URLRequest("http://192.168.0.19:800/output/" + elfich + ".mp3");
xlocalSound.load(xreq);
xlocalSound.addEventListener(Event.OPEN, iniciarSonido);
}
What about
loader.removeEventListener(Event.COMPLETE, cargacompletanew);
loader.removeEventListener(IOErrorEvent.IO_ERROR, onErrornew);
loader.close();
I think there was a problem with close() if you were trying to close it if nothing was loaded, so I'd try to put that inn try/catch block.
try
{
loader.close();
}
catch(e:Error)
{
loader("Oh noes!");
}

Load external swf into bytearray with adobe flash

How can I load external swf into bytearray with adobe flash AS3..
Here is my code..
var my_url1:URLRequest = new URLRequest("SWF/Lesson1.swf");
my_url1.method = URLRequestMethod.GET;
my_url1.contentType = "application/x-shockwave-flash";
var urlloader:URLLoader = new URLLoader(my_url1);
var myByteArray:ByteArray = new ByteArray();
urlloader.data = myByteArray;
It not works well. But it isn't give any error. How can I fix this problem?
You should listen for COMPLETE event and set the data format to BINARY:
var my_url1:URLRequest = new URLRequest("SWF/Lesson1.swf");
var urlloader:URLLoader = new URLLoader(my_url1);
urlloader.dataFormat = URLLoaderDataFormat.BINARY;
urlloader.addEventListener(Event.COMPLETE, function(event:Event):void
{
var myByteArray:ByteArray = URLLoader(event.target).data as ByteArray;
trace(myByteArray.length);
});

Flash / App Engine request data encoding problem (GET works, POST fails)

I'm new to Flash and trying to communicate with an app engine server but I'm having some encoding problems.
var playerLoader:URLLoader = new URLLoader();
playerLoader.addEventListener( Event.COMPLETE, function(e:Event) { doStuff(); } );
var requestVars:URLVariables = new URLVariables();
requestVars.q = "åäö";
var urlRequest:URLRequest = new URLRequest( serverUrl );
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = requestVars;
playerLoader.load( urlRequest );
This does not work, gives me some encoding errors on the server side. But if I switch POST to GET it automagically works.
Any clues what's going on and how I could use POST?
Thanks!
As far as I know, Actionscript's HTTP requests will natively be encoded in UTF-8.
I have it working this way for spanish chars:
var url:String = "historia.php";
var request:URLRequest = new URLRequest(url);
var requestVars:URLVariables = new URLVariables();
var now:Date = new Date();
requestVars.historia = "áóíúéñÁÓÍÚÉÑ";
request.data = requestVars;
request.method = URLRequestMethod.POST;
var urlLoader:URLLoader = new URLLoader();
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.load(request);
And on the PHP side (resumed for clarity):
header ('Content-type: text/html; charset=utf-8');
if(isset($_POST['historia']) {
$historia = mysql_real_escape_string(utf8_decode($_POST['historia']));
$historia = filter_var($historia, FILTER_SANITIZE_STRING);
// insert into DB...
}
I use utf8_decode() because the database is not configured with encoding UTF-8.
How are you handling the request on the server side? Maybe the problem lies on the server side.