Click to See Complete Forum and Search --> : Reception in PHP of xml data from Flash.


Jazztronik
10-05-2007, 01:34 PM
I need some help on this question.

I don't know how to handle the xml data a PHP document is sent from Flash. It can't be $_POST as it would try to split everything into variable-value pairs.

I googled for it and found in another forum this server variable: $HTTP_RAW_POST_DATA
But that thread was made in 2001! :eek: So I can't rely on it.

Could anyone please help me?

discorax
10-05-2007, 02:22 PM
so what are you passing out of Flash? You said XML...how are you doing that?

A little code here might help to clarify your issue.

Jazztronik
10-05-2007, 02:50 PM
Yes, Flash, as it's client-side, needs the use of a server-side language such as PHP, ASP and so on to store data in a database. And their best way to transfer the data to each other is by using XML.

This is a piece of Actionscript code I use to build the xml tree and send it to PHP by means of a special built-in Flash object, called XML as well:

submit_btn.onRelease = function() {
// Construction of XML tree in a String
var XMLtoSend:String = "<Register><UserName>" + username_ti.text + "</UserName><Email>" + email_ti.text+ "</Email><Password>" + password_ti.text + "</Password></Register>";
var objToSend:XML = new XML(XMLtoSend); // This XML object is assigned the string containing the xml data
objToReceive = new XML(); // XML object which will receive the xml data replied by server
objToReceive.onLoad = registrationResponse; // callback to assigned function when I get the response

// order to send data, and to receive an answer from the server with some xml data
objToSend.sendAndLoad("http://localhost/my_path_to_the_php_file/register.php",objToReceive);
// Player goes to frame "Waiting" until an answer from the server arrives
_root.gotoAndStop("Waiting");
};

I don't know how to start using the data sent by Flash in PHP.

I hope not to get you in a muddle.

discorax
10-05-2007, 05:33 PM
There are better ways to do this...You can pass variables to the PHP rather than writing out XML and then having PHP parse it out and give you return values, put it in a DB, etc....

Try something like this.


var xmlResponse:XML = new XML();
var xmlListener:Object = new Object();
xmlResponse.addListener(xmlListener);
xmlListener.onLoad = registrationResponse;

submit_btn.onRelease = function():Void{

var userNameVal:String = username_ti.text;
var emailVal:String = email_ti.text;
var passwordVal:String = password_ti.text;

xmlResponse.sendAndLoad("http://localhost/my_path_to_the_php_file/register.php?user="+userNameVal+"&email="+emailVal+"&pass="+passwordVal, xmlListener);

}

function registrationResponse(success){
if (success) {
// do something wtih the XML
}else{
trace("something bad happened with the PHP");
}

}


You can also try using LoadVars. Here is an example.
http://flashmove.com/forum/showthread.php?t=8393


So with you PHP code you just need to get those _GET vars from the URL like this
<?php
$username = $_GET['user'];
$email = $_GET['email'];
$password = $_GET['pass'];

//now do something with those values
$someValue = "some value here";
// and echo out some XML that you want flash to deal with

echo "<node>";
echo "<firstChild>".$someValue."</firstChild>";
echo "</node>";

?>

Jazztronik
10-06-2007, 05:01 AM
Thank you Discorax! :cool: :)

Yes I thought on LoadVars previously, but what if you need to send complex data to PHP? not just separate string/integer or other simple variables?

I haven't tried yet, but, is it possible to use an empty XML Object in Flash with the sole purpose to attach GET variables to it?

I know there's a way around without the need of XML that is Flash Remoting + AMFPHP, but I wanted to solve this question before getting into these technologies. Plus Flash Remoting is not free.

I got the Flash + Actionscript code from a real exercise in a book: "Macromedia Flash 8: Training from the Source", but the problem is this exercise uses ASP on server-side, and I just wanted to use PHP the same way. If ASP can, PHP must be able too itself, doesn't it?

discorax
10-07-2007, 03:53 PM
Now I have a better understanding of what you're trying to do here.

Take a look at how PHP parses XML and then you should have your answer. If you need to pass complex information into PHP XML is probably your best bet. There are lots of tutorials about parsing XML in PHP take a look at those, as for the response XML, the echo example I posted above is still the best way I've found to pass XML back into flash after PHP processing.

Good Luck, you're on the right track!

Jazztronik
10-08-2007, 02:39 PM
Yes, I've searched a lot, but all the tutorials I found were, apart from using Flash remoting, about PHP getting the xml data from an xml file, and then parsing it. I was looking for some way to do it on the fly: same way as you say when the response is back to Flash (Flash gets the echoed xml on the fly), but also viceversa.

I could try to do this:
Flash -> data.xml -> PHP,
but there must be some way to do it on the fly, like the response does:
Flash -> PHP

discorax
10-08-2007, 02:45 PM
Sounds like you really want a shared XML object. You could try playing with flash XMLconnector component. That may get you where you need to go.
http://livedocs.adobe.com/flash/mx2004/main_7_2/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Flash_MX_2004&file=00003048.html

If that doesn't work, the
Flash -> data.xml -> PHP <PROCESS XML> PHP -> data.xml -> Flash
would probably be your best bet.

discorax
10-08-2007, 03:05 PM
I haven't used this, but this technology may also accomplish what you need.

SWX
http://swxformat.org/documentation/#73

Jazztronik
10-14-2007, 01:05 PM
Thank you again!

How do I write an XML file (data.xml in the example) through Flash? I'm afraid my knowledge doesn't cover that issue. :o Is there any way to do it?

discorax
10-15-2007, 10:12 AM
Unfortunately Flash can not write out an XML file by itself. You need a server side programming language like PHP (which is why you're in this forum) to do that. My example above would help you get information to a PHP script.

Then you can use this script, or something similar to create an XML file that you can refer to later. Depending on what you're trying to do this could be a good route OR you can just echo the XML straight out and read it back into flash like my example above does. In addition to echoing it out you could also save it, that's the power of server-side scripting languages.



<?php
if(isset($_GET['myVar'])){

echo "Employee Data Posted";

$xmlfileName = $_GET['xmlfileName'];

$empName = $_GET['empName'];

$empAddress = $_GET['empAddress'];

$empSSN = $_GET['empSSN'];

$empCompany = $_GET['empCompany'];

$xml_dec = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";

$rootELementStart = "<employee>";

$rootElementEnd = "</employee>";

$xml_doc= $xml_dec;

$xml_doc .= $rootELementStart;

$xml_doc .= "<employeename>";

$xml_doc .= $empName;

$xml_doc .= "</employeename>";

$xml_doc .= "<employeeaddress>";

$xml_doc .= $empAddress;

$xml_doc .= "</employeeaddress>";

$xml_doc .= "<SSN>";

$xml_doc .= $empSSN;

$xml_doc .= "</SSN>";

$xml_doc .= "<company>";

$xml_doc .= $empCompany;

$xml_doc .= "</company>";

$xml_doc .= $rootElementEnd;

$default_dir = "xml_files/";

$default_dir .= $xmlfileName .".xml";

//Here I have taken default directory as xml_file directory.

// the following PHP script stores the XML in a file

$fp = fopen($default_dir,'w');

$write = fwrite($fp,$xml_doc);



Here is the result:

<?xml version="1.0" encoding="UTF-8"?>

<employee>

<employeename>NAME</employeename>

<employeeaddress>Topxml</employeeaddress>

<SSN>12345</SSN>

<company>TopXML</company>

</employee>


Hope that answers your question. Do a search for create XML with PHP and there are TONS of examples out there to help with that :)