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:
- json_encode(): http://php.net/manual/en/function.json-encode.php
- json_decode(): http://php.net/manual/en/function.json-decode.php
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} | |
?> |
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.
- gzcompress() - http://php.net/manual/en/function.gzcompress.php
- gzuncompress() - http://php.net/manual/en/function.gzuncompress.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
?> |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
?> |
Here is the complete class:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} | |
?> |
No comments:
Post a Comment