GAMP programming - Integrating PHP and GWT

I'm writing some software for doing accounting for volunteer organizations. For this case I decided to go with Google Web Toolkit as the frontend with a PHP backend on the web-server side. This combination works surprisingly well and GWT has performed just nice so far.

I have set up the entire environment on my laptop enabling me to work offline. The development environment is also great, as I have everything I need in Eclipse - PHP, database browser and of course the Java development environment.

Now the trick here is of course that GWT expects/supports a Java backend. This isn't really a problem, as all that GWT do is doing http calls to the server side. I write all my server side calls as I would have done regular web calls, and for PHP to access these post or get parameters is done using $_REQUEST variable.

For obtaining data from the PHP side, I use echo and json_encode. PHP has very good support for json encoding data - give json_encode method a list/map/object and it will encode it as a list or map. All public attributes in objects are encoded just as they were a map, so sending data structures from PHP to GWT is a breeze. On the GWT side it is equally simple to decode the data. Just like this:

PHP:
$ret = array();

$ret["values"] = AppConfig::CountValues();
$ret["columns"] = AppConfig::CountColumns();

echo json_encode($ret);

Which PHP sends out as:

{"values": [1000,500,200,100,50,20,10,5,1,0.5],
"columns": ["a1000","a500","a200","a100","a50","a20","a10","a5","a1","a_5"]}


On the GWT side I can do:

JSONValue value = JSONParser.parse(responseText);
JSONObject object = value.isObject();
JSONValue values = object.get(
"values");
JSONValue columnValue = object.get(
"columns");
JSONArray arrayValues = values.isArray();
JSONArray arrayColumns = columnValue.isArray();

for (int i = 0; i < arrayValues.size(); i++) {
JSONValue itvalue = arrayValues.get(i);
JSONValue itcolumn = arrayColumns.get(i);

String countValue = Util.str(itvalue);
counts.add(countValue);
countGivesColumn.put(countValue, Util.str(itcolumn));
}

This is basicly what you need to connect PHP and GWT. I found some minor strange features, like when I used the http builders in GWT for doing a POST I needed to set the following header:

builder.setHeader("Content-Type", "application/x-www-form-urlencoded");

If this header was omitted, PHP didn't want to see my post variables. GWT uses default text/plain, as spesified by the documentation. Besides this small obstacle the GWT and PHP integration works just fine Happy
|