Menu

Friday, March 15, 2013

Using compression in REST communication between applications

When you are working on a large project, there are alot of situations that the project is composed of more than one application. The problem here is how to solder everything together. There are alot of ways you can handle communication between applications, for example you can use XML, JSON, HTTP Requests, etc. If you are using PHP for building your project maybe the easiest way of handling communication is by using JSON.
There are two buildin function that you need to look at before starting:
We will use these two functions to serializing and deserializing the data that we need to send between applications.
So what we need now is methods for encoding data and sending the encoded data to a certain url:

<?php
class AppComm {
/**
* Sends data via a post request
* @param string $url
* @param array|stdClass $object
*/
public static function send($url, $object) {
$curl = curl_init($url);
$params = self::encodeParams($object);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_USERAGENT, self::USER.':'.self::API_KEY);
curl_setopt($curl, CURLOPT_POSTFIELDS, "params=$params");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 1);
curl_exec($curl);
curl_close($curl);
}
/**
* Converts an array or object to json string
* @param array|stdClass $object
* @return string
*/
private static function encodeParams($object) {
$params = json_encode($object);
return $params;
}
}
?>
view raw gistfile1.php hosted with ❤ by GitHub

Now we have two static methods:
- send - sends the json string via http post request
- encodeParams - convert an object or array to json string
Because we don't need to wait for the request to finish we add:
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 1);
This two lines of code mean that the connection will be broken after one second.
If there is alot of data that needs to be tranfered then we will need to use somekind of data compression. To do that in php we can use the zlip library.
<?php
/**
* Converts an array or object to json string
* @param array|stdClass $object
* @return string
*/
private static function encodeParams($object) {
$object = json_encode($object); // convert to JSON
$object = gzcompress($object, 9); // compress string
$object = base64_encode($object); // encode string with BASE64
$object = urlencode($object); // url encode string
return $object;
}
?>
view raw gistfile1.php hosted with ❤ by GitHub
We update the encodeParams method to handle zlip compression.
1. We convert the object to a json string
2. We compress that string using gzcompression
3. Because the compressed string has many bytes that connot be send via a post request, we need to encoded. We use base64 firs because we wont gain much if url encode it first.
4. We encode the string with urlencode, because we need to send it via a http post request.
To recieve compressed data from another application we need to decode the post prameter:

<?php
/**
* Decodes a compressed json string
* @return null|stdClass
*/
public static function recieve() {
if(isset($_POST['params'])) {
$params = $_POST['params'];
$params = base64_decode($params);
$params = gzuncompress($params);
$params = json_decode($params);
return $params;
}
return null;
}
?>
view raw gistfile1.php hosted with ❤ by GitHub


Here is the complete class:

<?php
class AppComm {
/**
* Sends data via a post request
* @param string $url
* @param array|stdClass $object
*/
public static function send($url, $object) {
$curl = curl_init($url);
$params = self::encodeParams($object);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_USERAGENT, self::USER.':'.self::API_KEY);
curl_setopt($curl, CURLOPT_POSTFIELDS, "params=$params");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 1);
curl_exec($curl);
curl_close($curl);
}
/**
* Converts an array or object to json string
* @param array|stdClass $object
* @return string
*/
private static function encodeParams($object) {
$object = json_encode($object); // convert to JSON
$object = gzcompress($object, 9); // compress string
$object = base64_encode($object); // encode string with BASE64
$object = urlencode($object); // url encode string
return $object;
}
/**
* Decodes a compressed json string
* @return null|stdClass
*/
public static function recieve() {
if(isset($_POST['params'])) {
$params = $_POST['params'];
$params = base64_decode($params);
$params = gzuncompress($params);
$params = json_decode($params);
return $params;
}
return null;
}
}
?>
view raw gistfile1.php hosted with ❤ by GitHub

No comments:

Post a Comment