JSON Code ExamplesHere are some Code Examples for common uses of JSON.
Function lxlabs_get_via_jsonSince you are interacting with a specific server, there is no need to provide all the variables all the time in the higher level functions. So we will write a function called
lxlabs_get_via_json. This function takes 4 parameters, and uses the JSON library to return a PHP object back.
- Variables:
- $protocol: The protocol to be used: Either https or http.
- $server : The Kloxo/HyperVM server's ipaddress.
- $port: the port: 7777/8 or 8887/8
- $param: This is a partial url that contains the the actual parameters. Our function will add the parameters that are common for every action. The partial url is of the form "action=simplelist&resource=domain"
- $return: It will decode the JSON string, and then return a proper object back.^
function lxlabs_get_via_json($protocol, $server, $port, $param)
{
$param = "login-class=client&login-name=admin&login-password=pass&output-type=json&$param";
$url = "$protocol://$server:$port/webcommand.php";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
$totalout = curl_exec($ch);
$totalout = trim($totalout);
require_once('JSON.php');
$json = new Services_JSON();
$object = $json->decode($totalout);
if (!is_object($object)) {
print("Fatal Error. Got a non-object from the server: $totalout\n");
exit;
}
return $object;
}
Function lxlabs_if_errorThis is a function to check if the return value from lxadmin/hyperVM is error or not.
- Variables:
- $json: The json object recieved from the server.
function lxlabs_if_error($json)
{
return $json->return === 'error';
}
Function lxlabs_get_and_print_select_variableThis is a high level function that will print a select block containing the resource values.
- Variables:
- $description: Description of the variable
- $remotevar: The resource name to be sent to Kloxo/HyperVM resource=resourceplan
- $localvar: The name of the same resource that's used in the add form. For instance, the resource plan is sent via the variable name "v-plan_name"
- Example usage:
- lxlabs_get_and_print_select_variable("Resource Plan", "resourceplan", "v-plan_name");
- lxlabs_get_and_print_select_variable("Server", "vpspserver_openvz", "v-syncserver");
function lxlabs_get_and_print_select_variable($description, $remotevar, $localvar)
{
// Send the remote variable to hypervm and get the result.
$json = lxlabs_get_via_json("http", $server, $port, "action=simplelist&resource=$remotevar");
if (lxlabs_if_error($json)) {
print("The server said, error. The message is: $json->message\n");
exit;
}
lxlabs_print_select($localvar, $description, $json->result);
print("<br> ");
}
Function lxlabs_print_selectThis function prints a select box so that when a customer chooses a display name, the corresponding unique name is sent via the form.
- Variables:
- $var: The lxadmin/hyperVm variable for the select box. For instance, v-plan_name
- $displayvar: The display name for the variable: "Resource Plan"
- $list: An associative array containing the display-name and unique-name The lxlabs_print_select will create a select box so that the relationship between them is maintained transparently, and the unique name is hidden from the customer.
- Example:
- lxlabs_print_select("v-plan_name", "Resource Plan", $list);
function lxlabs_print_select($var, $displayvar, $list)
{
print("Select a $displayvar");
print("<option value="$uniquename"]$displayname</option>"); }
Listing Resources ExampleNow that we have seen all the code examples we can see how we can list resources and perform more complex tasks. All resource objects in Kloxo/HyperVM will depend on other resources. For instance, to add a domain, you need to provide a
Resource Plan as an input. To provide the correct resource plan, you will have to first get a list of resource plans from Kloxo/HyperVM and then ask the client to select one. So understanding the lxadmin's listing policy becomes pivotal to writing a good interaction system. Kloxo/HyperVM now has a universal listing method, and it can be used on
any resource object, whether it be domain, client, vps, vpspserver_openvz.
Listing a resourceThe
action for listing is
simplelist and the resource name is sent via the
resource variable. So let us say you need a list of Resource Plans in Kloxo. The command would be like this:
$json = lxlabs_get_via_json("http", $server, "7778", "action=simplelist&resource=resourceplan");
The $json is a JSON object. And the actual list is contained in the result property.
$list = $json->result
For simplelist, the $json->result is an associative array. Why are we using an associative array, and not a simple array? The reason is that many resources have two distinct key values: The display name, and the unique name. For instance, let us take resource plans. Each customer can have his own resource plans, and the display can overlap. One customer using a plan called
silver doesn't preclude another from using the same name. So to distinguish between the two resource having the same display names, Kloxo/HyperVM will use an internal unique name by concatenating the display name with the client's name. So when you use the simple list, you are getting an associative array containing both the display name and the unique name. The format of the array is this:
$list[['unique_name'] = 'display_name';
The key idea is that you need not show the customer the unique name. In the select option, you only show the display name, and create the select box in such a way that when a customer chooses a
display name, the form will automatically send the
unique name. This can be accomplished quite easily. The function
API#Function lxlabs_print_select from above will create a select box in such a way that it will transparently maintain the relationship between
display name and the
unique-name$json = lxlabs_get_via_json("http", $server, "7778", "action=simplelist&resource=resourceplan");
if (lxlabs_if_error($json)) {
print("Server gave error: Message: $json->message.\n");
exit;
}
lxlabs_print_select("v-plan_name", "Resource Plan", $json->result);
You can use JSON
API#Function lxlabs_get_via_json function from above to get any listing.
$domain_list = lxlabs_get_via_json("http", $server, "7778", "action=simplelist&resource=domain");
$server_list = lxlabs_get_via_json("http", $server, "7778", "action=simplelist&resource=vpspserver_openvz");