Copy link to clipboard
Copied
From the 'Learning Actionscript 3' book I'm trying out reading/writing to an xml file.
Actionscript:
var txtFld:TextField = new TextField();
txtFld.width = 500;
txtFld.height = 350;
txtFld.multiline = txtFld.wordWrap = true;
addChild(txtFld);
var str:String = "<?xml version='1.0' encoding='utf-8'?>";
str += "<value>Sent from ActionScript</value>";
var xmlToSend:XML = new XML(str);
var req:URLRequest = new URLRequest("save_xml.php");
req.data = xmlToSend;
req.contentType = "text/xml";
req.method = URLRequestMethod.POST;
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(IOErrorEvent.IO_ERROR,
onIOError, false, 0, true);
urlLoader.addEventListener(Event.COMPLETE,
onComplete, false, 0, true);
urlLoader.load(req);
function onIOError(evt:IOErrorEvent):void {
txtFld.text = "XML load error.\n" + evt.text;
}
function onComplete(evt:Event):void {
try {
urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
urlLoader.removeEventListener(Event.COMPLETE, onComplete);
var loadedXML:XML = new XML(evt.target.data);
txtFld.text = loadedXML.stuff;
} catch (err:Error) {
txtFld.text = "XML parse error:\n" + err.message;
}
}
PHP:
<?php
if (isset($GLOBALS["HTTP_RAW_POST_DATA"])){
$data = xmldoc($GLOBALS["HTTP_RAW_POST_DATA"]);
$file = fopen("data.txt","wb");
fwrite($file, $data);
fclose($file);
if (!$file) {
echo("<stuff>Server unable to create file.</stuff>");
} else {
echo("<stuff>File saved.</stuff>");
}
}
?>
I'm using MAMP to test it, servers are running and both the flash file and the php script file are in the htdocs location. So I'm trying out http://localhost:8888/send_load_xml.swf and in the same location is save_xml.php and data.txt.
When I run the swf however I always get an 'XML Load Error #2032'. I've checked the name of the php file and the txt file and they are correct. Why am I getting this error?
Copy link to clipboard
Copied
your swf can't execute your php file.
you should upload to a server that already has php installed and working and test there or, test your own php installation and server setup outside of flash.
Copy link to clipboard
Copied
I've tested it with MAMP running, so a local php server. Also tested it on my own online location where php is installed and that didn't work eichter. Somewhere in the php or fla something isn't quite right. Someone able to find it?
Copy link to clipboard
Copied
Actually what worked was removing 'xmldoc' from the php script. So $data =$GLOBALS["HTTP_RAW_POST_DATA"]; instead of $data = xmldoc($GLOBALS["HTTP_RAW_POST_DATA"]);
Couldn't find anything about 'xmldoc'. Is it used for anything in php?
Anyway, the loading error went away, but then I got into problems with getting the return echoed values back. The textfield stayed empty even when saving the data.txt file worked.
I then placed the <stuff> tags between <root> tags and then flash got the sentences between the <stuff> tags back. Why does Flash need the returned xml output to be between <root> tags for it to see the content between the <stuff> tags? So why do I need to use echo("<root><stuff>Server unable to create file.</stuff></root>"); instead of cho("<stuff>Server unable to create file.</stuff>");?
Copy link to clipboard
Copied
xmldoc doesn't mean anything unless you define it.
var loadedXML:XML = new XML(evt.target.data);
txtFld.text = loadedXML.stuff;
should be
var loadedXML:XML = XML(evt.target.data);
txtFld.text = loadedXML.toString();