Login | Register

An evening with sendAndLoad, PHP and LiveDocs

This evening I once again learned an important lesson. The documentation in Flash MX 2004 is way better than it's predecessor, but the gold is in the comments.

Today I spent way too much time looking for a simple explanation. I wanted to send XML to a PHP script and have it return XML from a database. Sounds really easy, doesn't it?

This is the script I used in Flash:

// Setup the return function / Object
function myOnLoad(success:Boolean) {
  trace("success? "%2Bsuccess%2Bnewline%2Bthis);
}
var myLoginReply_xml:XML = new XML();
myLoginReply_xml.ignoreWhite = true;
myLoginReply_xml.onLoad = myOnLoad;

// Make a new XML object and send the data
var xmlObject:XML = new XML("Some TextNode");
xmlObject.sendAndLoad("http://www.flashmagazine.com/articlefiles/xmltest.php", myLoginReply_xml);


I did everything according to the built-in documentation, but it still did not work. For some reason, my PHP script did not receive the XML I was trying to send using the sendAndLoad command. My mistake was simple. I'm used to using the LoadVars, so I expected Flash to send the XML as a query string like LoadVars does. According to the official Flash Documentation it should work like this, but in fact it does not...

I talked with two skilled Flash developers I know to see if they had a solution. They both told me that they had tried using the sendAndLoad method, but given up on it since they could not get it to work according to the documentation. Interesting, isn't it? Poor documentation causes useful commands not to be used.

I thought that someone must have had this problem before, so I searched the Flashcoders archives but found no solution. I tried searching Macromedia.com and got several results, but none that seemed to solve my problem. After a long time and various attempts, I stumbled onto the solution searching with Google.

Macromedia are great on support ...


The support pages on macromedia.com are great and they solve almost every problem I have, but some times, I need to look up just one function to understand how it works. The built-in help system is quick and easy to use, but it lacks in term of examples. Many commands can be used in more than one way and there is often just one simple example. The quality of the examples also vary. Below each entry, there is a link to the online version of the help files: the Marcomedia LiveDocs. Click this and you can find the same help file, but with comments from other users. Here you will often find the solution to your problem and here was also the solution to my problem.

... but the Livedocs are not what they could be.


I've always been somewhat dissatisfied with how the LiveDocs are oganized and work. For each new version of the software, a brand new set of documentation is added, but none of the valuable comments are transferred. The best online documentation I've ever seen is for the serverside scripting language PHP. Just look at the entry for Arrays in LiveDocs. Here you will find three examples of how to use arrays, but none of them tells you how to make a multidimensional Array or an associative one. The solution to one of these can be found as a comment made by a user.

In addition, one of the examples contains an error. This error is pointed out by a user in the comments, and a Macromedia employee writes a comment saying 'Very good point, thanks for pointing that out, I'll fix the documentation'. For some reason, the entry is still not corrected.

Just to compare this to the PHP help system, here is their entry on Arrays. Below the official explanation, you'll find 29 user comments and they are all helpful in some way! The oldest comment is from 1999 and back then PHP was in version 3. PHP is now in version 5, but the command is still the same and only comments that still apply are kept by the moderators maintaining the help system.

Macromedia? Here's my friendly advice: make sure someone reviews ALL the comments in the Flash MX 2004 Livedocs and copy any valid information into the docs for the upcoming Flash 8. Some comments, such as the one that saved my day should even be included in the documentation itself. Also: hire one or more people with above average Flash skills to maintain and review all new comments. The last comment on your Array page belongs in a Forum, not in a help system.

Enough ranting. Here's the solution:


The LiveDocs say that sendAndLoad takes its XML and '... sends it to the specified URL using the POST method ...' The problem is that sendAndLoad method sends the XML as RAW POST data, rather than as ordinary POST variable/value pairs like it does for LoadVars. You cannot pick this up on the server as part of any of the ordinary commands for getting GET/POST variables since it's never sent as such.

I have no idea who is behind the signatures 'dvhh' and ' No screen name', but together they provide a solution not only for PHP but also for ASP.net.

Here is the solution for ASP.net:

use Request.BinaryRead(Request.TotalBytes) to get the POST XML content.



Here is the PHP solution:

print file_read_contents('php://input') ;



That's just about it. I made a PHP file that outputs the same data that were sent in a different form, so that you can check that what you are sending are being received by the server and sent back. Download the PHP file here.

Now if Macromedia does not take my advice about 'golden nuggets of info' into the upcoming documentation, I will at least have this article for future references. Who knows? Maybe it will help others to?

UPDATE: Patrick Mineault points out that one can also "use the $HTTP_RAW_POST_DATA global [variable] which saves a function call and the file wrappers", an easier approach than I suggest in this article. He also suggests using AMFPHP as a better alternative if you don't explicitly need to use XML. AMFPHP is an alternative to Macromedia's Remoting product that is Open Source and requires minimal installation. According to Patrick, "AMFPHP is stable now, it's in its last release before the official 1.0". Patrick is one of the programmers for the AMFPHP project.
Check out AMFPHP here

 

About Jens C Brynildsen

Jens has been working with Flash since version 3 came out. Since then, he's been an active member of the Flash community. He's created more than a hundred Flash games (thus the name of his blog) but he also creates web/standalone applications, does workshops and other consulting. He loves playing with new technology and he is convinced that the moment you stop learning you die (creatively speaking). Jens is also the Editor of this website.

Get new stories first

Click to follow us on Twitter!

 

Comments

No comments for this page.

Submit a comment

Only registered members can comment. Click here to login or here to register