I Need to save xml from Flex to file on server with PHP, which PHP script?
I have a Flex app which is taking XML data and posting it to be saved to a file on the server, where it can be read later. Using tip found here on SO, I have the Flex part down (I think), but I am not a PHP programmer so I am unclear as to what should be in the PHP that handles the URLRequest:
protected function saveBtn_clickHandler(event:MouseEvent):void {
var saveData:XML = new XML();
saveData = myManager.exportFullXML();开发者_开发技巧
trace(saveData);
var uploader:URLLoader = new URLLoader();
var data:URLVariables = new URLVariables();
data.xml = saveData.toXMLString();
trace(data.xml);
var req:URLRequest = new URLRequest("http://locahost/saveXML.php");
req.data = data;
uploader.load(req);
}
First of all ensure that your request is a post one (it isn't mandatory, but it is a good practice as XMLs tend to be lengthy):
req.method = URLRequestMethod.POST;
The PHP part is simple as PI. You get the received data from the $_POST
superglobal and you write it to your file with file_put_contents
.
<?php
file_put_contents('path/to/file', $_POST['data']);
?>
Note: remember that anyone can post to that PHP file, so you need to implement some security mechanism to ensure you only save what your application sends.
精彩评论