Fetching URL-Encoded Variables
PHP can output standard key-value pairs, which ActionScript 3 parses directly using the URLVariables class.
<?php
$operationStatus = "Initialized";
$responseMsg = "Success";
echo "status=" . $operationStatus . "&message=" . $responseMsg;
?>submitBtn.addEventListener(MouseEvent.CLICK, fetchTextData);
outputTxt.text = "Fetching...";
function fetchTextData(evt:MouseEvent):void {
var dataLoader:URLLoader = new URLLoader();
dataLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
dataLoader.load(new URLRequest("http://localhost/backend/text_endpoint.php"));
dataLoader.addEventListener(Event.COMPLETE, handleTextResult);
}
function handleTextResult(evt:Event):void {
var parsedVars:URLVariables = URLVariables(URLLoader(evt.currentTarget).data);
outputTxt.text = "Current Status: " + parsedVars.status + "\n";
outputTxt.text += "Response: " + parsedVars.message;
}Parsing XML Output
When PHP generates an XML structure, AS3 can map the loaded string into an XML object for easy traversal.
<?php
header("Content-Type: text/xml");
echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<gallery>';
echo '<image>photo_alpha.jpg</image>';
echo '<image>photo_beta.jpg</image>';
echo '</gallery>';
?>submitBtn.addEventListener(MouseEvent.CLICK, fetchXmlData);
function fetchXmlData(evt:MouseEvent):void {
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("http://localhost/backend/xml_endpoint.php"));
xmlLoader.addEventListener(Event.COMPLETE, handleXmlResult);
}
function handleXmlResult(evt:Event):void {
var responseXml:XML = XML(URLLoader(evt.currentTarget).data);
outputTxt.text = responseXml.toXMLString();
}Transmitting Data via GET
Appending parameters to the query string allows AS3 to send data to a PHP script using the HTTP GET method.
System.useCodePage = true;
submitBtn.addEventListener(MouseEvent.CLICK, sendGetRequest);
function sendGetRequest(evt:MouseEvent):void {
var getRequest:URLRequest = new URLRequest("http://example.com/api/process.php");
getRequest.method = URLRequestMethod.GET;
getRequest.data = "itemId=42";
var getLoader:URLLoader = new URLLoader();
getLoader.load(getRequest);
getLoader.addEventListener(Event.COMPLETE, handleGetResult);
}
function handleGetResult(evt:Event):void {
outputTxt.text = URLLoader(evt.currentTarget).data;
}Transmitting Data via POST
For larger payloads or secure transmissions, assign a URLVariables object to the URLRequest.data property and set the method to POST.
System.useCodePage = true;
submitBtn.addEventListener(MouseEvent.CLICK, sendPostRequest);
function sendPostRequest(evt:MouseEvent):void {
var postVars:URLVariables = new URLVariables();
postVars.username = "admin";
postVars.token = "x83jkd";
var postRequest:URLRequest = new URLRequest("http://example.com/api/process.php");
postRequest.method = URLRequestMethod.POST;
postRequest.data = postVars;
var postLoader:URLLoader = new URLLoader();
postLoader.load(postRequest);
postLoader.addEventListener(Event.COMPLETE, handlePostResult);
}
function handlePostResult(evt:Event):void {
outputTxt.text = URLLoader(evt.currentTarget).data;
}