A PHP exampleΒΆ

This implementation uses the pecl_http package.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php

$my_server = array(
    "url" => "http://127.0.0.1:8080",
    "username" => "admin",
    "password" => "admin");

function xml_config_get($varname) {
    global $my_server;
    $url = $my_server{'url'}."/api/config/get";
    $params = "<parameters><varname>$varname</varname></parameters>";

    $response = http_post_data($url, $params,
        array(
            "httpauth" => $my_server{'username'}.':'.$my_server{"password"},
            "headers" => array(
                "Content-Type" => "text/xml"
            )
        ));
    list($headers, $response) = split("\r\n\r\n", $response, 2);
    $xml = simplexml_load_string($response);
    return $xml;
}

function json_config_get($varname) {
    global $my_server;
    $url = $my_server{'url'}."/api/config/get";
    $params = json_encode(array("varname"=>$varname));

    $response = http_post_data($url, $params,
        array(
            "httpauth" => $my_server{'username'}.':'.$my_server{"password"},
            "headers" => array(
                "Accept" => "text/javascript",
                "Content-Type" => "application/json",
            )
        ));
    list($headers, $response) = split("\r\n\r\n", $response, 2);
    $r = json_decode($response, true);
    return $r{'result'};
}

print "xml result = ".xml_config_get("company_name")."\n";
print "json result = ".json_config_get("company_name")."\n";

?>

Previous topic

A Delphi example

Next topic

A step-by-step tutorial

This Page