PDA

View Full Version : Passing xml to PHP



zerga
June 5th, 2007, 11:02 PM
Hi,
I've tried looking through many threads and such, but I have yet to find a solution to my problem. Mainly, I need help with my AS3 to send xml from flash to a php script and reload the data. I've gotten a successful loading of php to flash, but I have yet to successfuly send xml to php. Any simple code or link to a thread or something would be extremely helpful. Thanks in advanced!

Huinoio
June 6th, 2007, 01:49 AM
This could be a problem for me to help, since I'm using .net exclusively. I've also had many problems with this same thing. I don't know how it will work with .php, but I did post some stuff here:
http://www.kirupa.com/forum/showthread.php?t=261587

good luck

waffe
June 6th, 2007, 03:03 AM
Hey Zerga your in luck, I just spent two days figuring this out for flash AS3 and I will now give to you what I have found :geek:

In an .as file:



public function saveMemory() {
var request:URLRequest = new URLRequest("http://www.name.com/save.php");
request.data = xmlXML;
request.contentType = "text/xml";
request.method = URLRequestMethod.POST;

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, handleResponse );
loader.load(request);
//trace("xmlXML= "+xmlXML)
}

public function handleResponse(event:Event):void {
try {
// Attempt to convert the server's response into XML
var success:XML = new XML( event.target.data );

// Inspect the value of the success element node
if ( success.toString( ) == "1" ) {
//_message.text = "Saved successfully.";
trace("Saved successfully.");
} else {
//_message.text = "Error encountered while saving.";
trace("Error encountered while saving.");
}
} catch (e:TypeError) {
// Display an error message since the server response was not understood
//_message.text = "Could not parse XML response from server.";
trace("Could not parse XML response from server.");
}
}


Next in your .php script named save.php:



$filename = "xmlfilename.xml";
$raw_xml = file_get_contents("php://input");
$fp = fopen($filename, "w");
fwrite($fp, $raw_xml);
fclose($fp);

// Return success code to Flash
echo "<success>1</success>";
?>


Add the above flash code to your xml loading flash code and with a little luck you will have it working.

waffe