« Parsing emails in ColdFusion if you can't use CFPOP | Main | Finally, a (frustratingly easy) fix for "null null" errors »

Accepting Complex Data Types from PHP/ASP in a ColdFusion 7 Web Service

At work I've been preparing a new web service for our licensees to use. It's not just a content feed, like most examples of web services that you hear about; instead, it's a financial tool, where the licensee offers the tool's interface on their site, calls our site with the user's data, and then displays the calculated results that we send back. I decided that in order to make the licensee's own development work go smoothly, we should test out the new service ourselves from another server in another programming language-- we should eat our own dog food, in other words. The challenges that we found was in generating and serializing complex datatypes in PHP, and in deserializing those datatypes back to structs in our web service. Here's how we resolved it.

It's not too hard to create a struct-like object in PHP (they're called classes). The problem arises when you pass complex data from one web server to another, whether it be via POST or GET, you generally have to serialize your data (e.g., convert it to text) before passing it. So what format can you use?

We figured we would have to serialize our objects with either JSON or XML. You can convert your PHP classes to JSON with the JSON.php package, then send them off to your web service. Then in our web service, we changed our struct's arguments so that they didn't have a type defined. We then checked whether the string receives is a JSON or XML object (by looking to see whether the first character is a curly bracket "{" or by using IsXML() ).

Once you've determined the format of the data you received, you can convert it to a struct. ColdFusion 8 has the very handy functions DeserializeJSON, but in ColdFusion 7 you will need to use 3rd-party code like Czebotar and Messier's JSON.cfc.

Post a comment