Skip to main content
Inspiring
February 29, 2020
Question

AS3 POST to PHP - help needed.

  • February 29, 2020
  • 1 reply
  • 823 views

I am new to talking to PHP.

 

Trying to test sending data to a .php page both on a localhost server and on a remote server

 

I am trying following code:

 

AS3:

 var myData:URLRequest = new URLRequest("http://127.0.0.1:8080/testtom.php");
var variables:URLVariables = new URLVariables();
variables.username = "abc_request";
variables.amount = "5";
myData.data = variables;
myData.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
//loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, dataOnLoad);
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
loader.load(myData);
 
 
function dataOnLoad(evt:Event){
    trace('Completed');
}
function httpStatusHandler(event:HTTPStatusEvent){
results_txt.text = "httpStatusHandler: " + event;
result2_txt.text ="status: " + event.status
    }
 
PHP:

<?php

echo $_POST["username"]."post";
echo $_POST["amount"]."post";
echo $_GET["username"]."get";
echo $_GET["amount"]."get";

echo "hello world 808080";
?>

 

When sending to the localhost server I am getting an HTTPStatus of 200 back - suggesting something is working.

If I set loader.dataFormat = URLLoaderDataFormat.TEXT;  to URLLoaderDataFormat.VARIABLES

I get an error:

Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.

 

which suggests that when I use URLLoaderDataFormat.TEXT; that the data is being sent.

However I can't get the .php page to update.

 

Any help greatly appreciated.

 

Best Tommy Banana

 

 

This topic has been closed for replies.

1 reply

Inspiring
February 29, 2020

So we have tested the .PHP page and it works fine receiving a GET message from html.

eg clicking on the link 

 ourserver/testtom.php?username=TESTUSER&amount=123

gets the variables displayed on the php page in the GET section using the php code above.

So the problem is in getting the .swf file to talk to the .php correctly.

Inspiring
March 1, 2020

So in case anyone else ends up here. This code worked:

 

https://tournasdimitrios1.wordpress.com/2010/09/18/two-way-communication-between-flash-and-php-exchanging-variables/

 

So now I have the POST method working from AS3 to PHP and getting data back I can now try and work out how

to build the thing I need.... using JSON!

 

NB For it to work I needed to have both the .php and .swf file in the same directory online and not to use an absolute URL.

Inspiring
March 1, 2020

I tell a lie - it does work with an absolute URL!