1
Kloxo-MR Development / Kloxo API
« on: 2017-06-11, 16:59:05 »
Kloxo-API (taken from LxCenter Wiki)
Kloxo/HyperVM API
JSON
Our JSON based API provides the most comprehensive and robust interface for external programs to interact with Kloxo/HyperVM, and all our future integration efforts will be centered around the theme of JSON. Please check JSON for a better idea of what exactly is JSON, and how the LxCenter API uses it for communication.
What is JSON
JSON, is an object string notation. For people who are familiar with the serialize function available in the scripting languages, JSON wouldn't be anything new. The reason for using JSON instead of the native php serialization is that JSON will work across languages. The advantage JSON provides over traditional XML or mere url notation output is that, with JSON all interactions happens via objects, and you don't need to do any output parsing. Kloxo & HyperVM will be outputing direct objects, and these objects will be directly accessible in your code, whatever language it is written in.
Initialization Prerequisites
There is one minor drawback, and it is that the language that you use has to support JSON serialize/unserialize befor you can utilize our new API. PHP natively supports JSON via the json_serialize and json_unserialize functions from version 5.2.1 onwards, . For pre-5.2.1, you can install the JSON serialize, unserialize functions that are implemented in PHP itself. You can get it from here http://mike.teczno.com/JSON.tar.gz
The basic interaction between Kloxo/HyperVM happens like this: Let us say you want to get a list of domains from Kloxo. The API command is simplelist. To get the list, you send the simplelist command to Kloxo, and get the output.
Please note the output-type variable. You will have to specify this if you want to use the JSON interface. This is necessary to provide backward compatibility. If you don't specify the output-type=json, Kloxo will use the old style url notation to print the output, and so the older programs that used to interface with Kloxo via this method will work fine.
The output of the above command would be json encoded string. To get the actual object, unserialize it via the json_unserialize.
As I had explained, $result is a pure PHP object, and contains three values. $return, $result, and $message. The $return is a string and is either "error" or "success". $message is also a string which would be the error/success messages: For instance: "Error_login_error". If you send anything to Kloxo/HyperVM, you will have to do proper error checking and warn the customer of the problem.
If the return is success, then the data you seek will be present in the result variable, and in this particular case -that is the simplelist command- the data will be available as an associative array.
Kloxo/HyperVM API
JSON
Our JSON based API provides the most comprehensive and robust interface for external programs to interact with Kloxo/HyperVM, and all our future integration efforts will be centered around the theme of JSON. Please check JSON for a better idea of what exactly is JSON, and how the LxCenter API uses it for communication.
What is JSON
JSON, is an object string notation. For people who are familiar with the serialize function available in the scripting languages, JSON wouldn't be anything new. The reason for using JSON instead of the native php serialization is that JSON will work across languages. The advantage JSON provides over traditional XML or mere url notation output is that, with JSON all interactions happens via objects, and you don't need to do any output parsing. Kloxo & HyperVM will be outputing direct objects, and these objects will be directly accessible in your code, whatever language it is written in.
Initialization Prerequisites
There is one minor drawback, and it is that the language that you use has to support JSON serialize/unserialize befor you can utilize our new API. PHP natively supports JSON via the json_serialize and json_unserialize functions from version 5.2.1 onwards, . For pre-5.2.1, you can install the JSON serialize, unserialize functions that are implemented in PHP itself. You can get it from here http://mike.teczno.com/JSON.tar.gz
- Note: You don't have to do anything with Kloxo/HyperVM. Kloxo/HyperVM uses their own PHP 5.2, and has everything built-in.
The basic interaction between Kloxo/HyperVM happens like this: Let us say you want to get a list of domains from Kloxo. The API command is simplelist. To get the list, you send the simplelist command to Kloxo, and get the output.
Code: [Select]
$string = url_get("webcommand.php?login-class=client&login-name=admin&login-password=pass&output-type=json&action=simplelist&resource=domain");
Please note the output-type variable. You will have to specify this if you want to use the JSON interface. This is necessary to provide backward compatibility. If you don't specify the output-type=json, Kloxo will use the old style url notation to print the output, and so the older programs that used to interface with Kloxo via this method will work fine.
The output of the above command would be json encoded string. To get the actual object, unserialize it via the json_unserialize.
Code: [Select]
require_once('JSON.php');
$json = new Services_JSON();
$result = $json->decode($string);
As I had explained, $result is a pure PHP object, and contains three values. $return, $result, and $message. The $return is a string and is either "error" or "success". $message is also a string which would be the error/success messages: For instance: "Error_login_error". If you send anything to Kloxo/HyperVM, you will have to do proper error checking and warn the customer of the problem.
Code: [Select]
if (lxlabs_if_error($result)) {
print("Server gave error and we cannot continue, The message was $result->message\n");
exit;
}
If the return is success, then the data you seek will be present in the result variable, and in this particular case -that is the simplelist command- the data will be available as an associative array.
Code: [Select]
// Else clause for the above if.
print("Success: The server returned these values:");
print_r($result->result);