PDA

View Full Version : XML & PHP


indirect
02-07-2001, 01:37 PM
I'm trying to send an XML document from a flash movie to a PHP3 script via post. I'm having trouble recieving the posted XML on the PHP end. The Script I have reports the the $HTTP_POST_VARS is a string but is empty. I also tried looking for a temp file but no luck there either. Can anyone help?

Jesse
02-09-2001, 10:13 AM
Try changing the Variable method to GET for debugging. that way you can see the XML being passed in the URL bar.

Cheers

Jesse

neeld
03-04-2001, 09:29 AM
Assuming he's sending it through the XML.send() method, he can't send it GET.

The problem is that Flash is sending the XML without a name. PHP is doing some "funny" conversion.

It is trying to split it into name value pairs

<person name="fred">
<age>29</age>
<person>

becomes

NAME = <person_name
VALUE = "fred"><age>29</age><person>

go figgure
Neeld

indirect
03-05-2001, 12:29 PM
I figured it out. What it was is that I was not using the swf in an HTML file. You can't simply test the swf within flash you have to use in inside a browser window (f12) for it to actually send the XML. Only then will it be avalible as the $HTTP_POST_VARS variable.

wolf
07-16-2001, 07:35 PM
How did you guys fix the problem with php spliting xml into name value pairs ?

I more or less have this:
<?
$filename = "xmloutput.txt";
$fp = fopen( $filename,"w+");
while( list($key, $val) = each($HTTP_POST_VARS) ){
fwrite( $fp, "$key :: $val \n");
}
fclose( $fp );
?>
and I get odd things like this:
(KEY , VALUE)

<LOGIN_password :: \"barkbark\" username=\"myDog\" />

when it should look likes this:

<LOGIN password="barkbark" username="myDog" />

Any ideas folks ? URL encoding and loadVariables I know how to do, but this xml thing is really confusing me.

-- Wolf

[Edited by wolf on 07-16-2001 at 02:42 PM]

indirect
07-17-2001, 12:27 PM
all I can say is, when getting output from other applications, you usually have to parse out alot of things.
It looks like your going to have to use stripslashes(), and a few str_replace()'s .

Good Luck

wolf
07-17-2001, 02:17 PM
Posting this to be thorough. This is what I found:

In PHP $HTTP_RAW_POST_DATA only exists when the mime type of the POST is unrecognized. This is a problem when it's flash submitting XML.

Here's a informative link:
http://marc.theaimsgroup.com/?l=php-general&m=98923317720300&w=2

Apparently it's a PHP limitation and has nothing to do with Flash. Unsure if the PHP developers plan on a fix.

-- WOLF

wolf
07-17-2001, 02:18 PM
Posting this to be thorough. This is what I found:

Supposively, since XML is not normal url encoded post data - it should be stored in $HTTP_RAW_POST_DATA. But...

In PHP $HTTP_RAW_POST_DATA only exists when the mime type of the POST is unrecognized. This is a problem when it's flash submitting XML.

Here's a informative link:
http://marc.theaimsgroup.com/?l=php-general&m=98923317720300&w=2

Apparently it's a PHP limitation and has nothing to do with Flash. Unsure if the PHP developers plan on a fix.

-- WOLF

nicewebguy
07-17-2001, 05:36 PM
This is NOT a problem with PHP. Flash is sending meta information that does not match the content of the data it's sending. The application/x-www-form-urlencoded content-type is reserved exclusively for data that is actually url encoded (which xml data from Flash is not). I actually wish it was something wrong with PHP, because it would get fixed a lot faster if it was!

Anyway the kludge is to do some parsing in PHP before invoking the XML engine. The faulty content-type is causing PHP to parse the data as if it were url encoded ('attribute1=value1&attribute2=value2...') and place it in $HTTP_POST_VARS instead of $HTTP_RAW_POST_DATA where the XML engine expects it. It's trivial to set things right once you know what's gone wrong:

$s = '';

while(list($key, $val) = each($HTTP_POST_VARS))
{
$s .= $key . '=' . $val;
}

$s = str_replace('_', ' ', stripslashes($s));

$HTTP_RAW_POST_DATA = $s;

Then just proceed as normal, invoking your XML parser of choice...

BTW, anyone interested in building XML clients and servers with Flash and PHP should take a look at XML-RPC (Remote Procedure Call) at http://www.xml-rpc.com. There are helpful ActionScript and PHP libraries that make this much easier (after you use the above fix!).

Good luck,
doug :)

wolf
07-17-2001, 07:40 PM
Well, I have two tidbits of information. Thanks to nicewebguy for reminding of of the content issue.

You should not have to have a php script that parses the data back together if you set the content type of the xml to "text/xml". This was a bug in version 5,0,30,0 (the initial released version with the dev studio). See link: http://www.macromedia.com/support/flash/ts/documents/xml_content_type.htm It always sends a content type of "application/x-www-form-urlencoded". But in version 5,0,42,0 (only through the webbrowser) of the player you have a new XML member called contentType that I can set the type to "text/xml". Now, when I do this $HTTP_RAW_POST_DATA will be set with my XML and not chopped into nasty key value pairs.

And the second minor tidbit, for PHP developers: In version 4.0.7 there will be a config directive to force $HTTP_RAW_POST_DATA to stay set no matter what.

For those of you interested, here's my code (and it seems to work for me - heavily borrowed from macromedia example):

Flash:

on (release) {
// A. Construct a XML document with a LOGIN element
loginXML = new XML();
loginElement = loginXML.createElement("LOGIN");
loginElement.attributes.username = "MyDog";
loginElement.attributes.password = "barkbark";
loginXML.appendChild(loginElement);
// this only works in version 5,0,42,0 or greater (test in web browser)
loginXML.contentType = "text/xml";

// B. Construct a XML object to hold the server's reply
loginReplyXML = new XML();
loginReplyXML.onLoad = onLoginReply;

// C. Send the LOGIN element to the server,
//place the reply in loginReplyXML
loginXML.sendAndLoad("xmlreader.php", loginReplyXML);

}

xmlreader.php PHP CODE:

<?
// make sure you have the proper permissions to write to this file
$filename = "xmloutput.txt";
$fp = fopen( $filename,"w+");
fwrite ( $fp, "$HTTP_RAW_POST_DATA" );
fclose( $fp );
print '<?xml version="1.0"?><result>LOGIN OK</RESULT>';
?>

and I end up with an xmloutput.txt file with this in it:
<LOGIN password="barkbark" username="MyDog" />

Hope this helps. Thanks to everyone who helped me figure this one out.

nicewebguy
07-17-2001, 07:59 PM
Way to go, Wolf! A much better solution!

doug

Evil
08-28-2001, 10:30 PM
does wolf's PHP script work for anyone else?

when i do it exactly as is, it writes a BLANK xmloutput.txt file and it's driving me NUTS!

when i change $HTTP_RAW_POST_DATA to $HTTP_POST_VARS it wites an xmloutput.txt file that contains only the word "Array" (with no quotes)

i'm at a loss, could my server not fully support XML? they are using PHP Version 4.0.4pl1 and it says on the info.php that XML Support is active...

any thoughts?

nicewebguy
09-03-2001, 04:37 AM
I don't know about wolf's script working, but the second line below will keep PHP from screwing up the XML data:

xml_ob = new XML();
xml_ob.contentType = "text/xml";

Tibberous
06-12-2004, 09:48 PM
Somone really needs to write a tutorial on this, as I have spent about 4 hours trying to find a solution. Thankfully Wolf's method worked for me...

michaelphipps
02-04-2005, 12:33 PM
$HTTP_POST_RAW_DATA is not available unless you use a particular enctype when you post. To get around this problem, collect your raw post data using this method...

$postdata = file_get_contents("php://input");

It more efficient, and works great - stop going bald for no reason!

More information can be found at: http://www.php.net/manual/en/wrappers.php.php