138 Part I: Core Ideas
The transmission is shown next and can be found at http://ajaxref.com/ch4/
yamlrequest.html.
On the server side, the received YAML content must be decoded. The approach is
similar to the JSON example: read the raw data stream and employ a library to decode the
data into variables for use. In this small PHP fragment, we use a library called SPYC
(http://spyc.sourceforge.net/) to perform the YAML decode:
$payloadString = $GLOBALS['HTTP_RAW_POST_DATA'];
$payloadArray = array();
if (strstr($headers["Content-Type"], "text/x-yaml"))
{
require_once('spyc.php');
$payloadArray = Spyc::YAMLLoad($payloadString);
}
Similar to JSON, YAML is fairly terse and, with the right libraries, easy enough to use.
However, later on when considering the response type, YAML will prove to be less than
ideal. For now, trust that unless you are a Ruby fanatic, YAML will probably not be in
your future.
Plain Text
Given the simplicity of YAML, you might wonder: why not send the data in a very simple
plain text format, maybe even comma-separated values (CSV)? This would certainly be the
smallest transmission format without compression. In fact, it is possible to do this, but
always ensure to escape any separators in the encoded payload. For example given:
"I love commas," said the comma happy author. "Please, no!" exclaimed the editor.