-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapicall-example.php
More file actions
165 lines (151 loc) · 5.3 KB
/
apicall-example.php
File metadata and controls
165 lines (151 loc) · 5.3 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/*
* Example PHP application to make calls on
* Kylone MicroCMS XML API, v2.2.0
* Revision 5 October, 2018
*/
// posts data with cURL and get XML document as response
function doc_post($url, $doc)
{
$ct = curl_init();
curl_setopt($ct, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ct, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ct, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ct, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ct, CURLOPT_TIMEOUT, 20);
curl_setopt($ct, CURLOPT_MAXREDIRS, 2);
curl_setopt($ct, CURLOPT_URL, $url);
curl_setopt($ct, CURLOPT_POST, 1);
curl_setopt($ct, CURLOPT_POSTFIELDS, array("xml" => $doc));
curl_setopt($ct, CURLOPT_USERAGENT, "API Client, MicroCMS-XML-API/v2.2.0");
curl_setopt($ct, CURLOPT_HTTPHEADER, array('Content-Encoding: UTF-8'));
curl_setopt($ct, CURLOPT_ENCODING , "gzip");
$res = curl_exec($ct);
curl_close($ct);
return $res;
}
// converts 'act=val&key=val'
// to '<args><atr name="act">val</atr><atr name="key">val</atr></args>'
function arg_construct($argstr)
{
$l = "";
$v = explode("&", str_replace(array("\\", "0x"), "", $argstr), 20);
$c = count($v);
for ($i = 0; $i < $c; $i++) {
if ($v[$i] != "") {
$x = explode("=", $v[$i], 2);
$l .= '<atr name="'.htmlspecialchars($x[0], ENT_QUOTES).'">';
$l .= htmlspecialchars($x[1], ENT_NOQUOTES);
$l .= '</atr>';
}
}
return '<args>'.$l.'</args>';
}
// creates XML document with target function name, parameters list
// and with session ID if given
function doc_construct($fname, $argxml, $ssnid = "", $xfile = false)
{
$s = ($ssnid != "") ? '<atr name="s">'.$ssnid.'</atr>' : '';
$x = '<?xml version="1.0"'.'?'.'>';
$x .= '
<cLst>
<container>
<operation>
<type>request</type>
<cookies>'.$s.'</cookies>
</operation>
<data model="struct">
<request>'.htmlspecialchars($fname, ENT_QUOTES).'</request>
'.arg_construct($argxml).'
</data>
</container>
</cLst>
';
if ($xfile !== false)
file_put_contents($xfile."_".$fname."_request.xml", $x);
return $x;
}
// performs inquiry and returns response as it is (XML document)
function do_query_and_get_doc($url, $doc)
{
return doc_post($url, $doc);
}
// performs inquiry and returns response as php-object
// after doing some sanitiy checks
function do_query_and_get_obj($url, $doc, $xfile)
{
$resp = doc_post($url, $doc);
if ($xfile !== false)
file_put_contents($xfile."_login_response.xml", $resp);
$flags = LIBXML_COMPACT | LIBXML_NOBLANKS | LIBXML_NOCDATA | LIBXML_NOEMPTYTAG;
$flags |= LIBXML_NONET | LIBXML_PEDANTIC | LIBXML_PARSEHUGE;
$xobj = simplexml_load_string($resp, "SimpleXMLElement", $flags);
if (!isset($xobj->container->operation->type))
return false;
if ((string)$xobj->container->operation->type != "response")
return false;
if (!isset($xobj->container->operation->status))
return false;
if ((string)$xobj->container->operation->status != "ok")
return false;
return $xobj;
}
// creates login document and gets sessinid with inqury
function do_login($url, $uname, $pass, $xfile)
{
$logindoc = doc_construct("login", "username=".$uname."&password=".$pass, "", $xfile);
$response = do_query_and_get_obj($url, $logindoc, $xfile);
if ($response === false)
return false;
if (!isset($response->container->operation->cookies))
return false;
$cvals = false;
foreach ($response->container->operation->cookies->children() as $node) {
$n = $node['n'];
$cvals["$n"] = (string)$node;
}
return (isset($cvals["s"]) ? $cvals["s"] : false);
}
// performs login, apicall and logout
function do_apicall($url, $uname, $pass, $fname, $argstr, $xfile)
{
// performs login and gets sessinid if possible
$ssnid = do_login($url, $uname, $pass, $xfile);
if ($ssnid === false)
return false;
// performs apicall for target function with parametes and sessionid
$calldoc = doc_construct($fname, $argstr, $ssnid, $xfile);
$callres = do_query_and_get_doc($url, $calldoc);
if ($xfile !== false)
file_put_contents($xfile."_".$fname."_response.xml", $callres);
// performs logout without considering the previous result
$logoutdoc = doc_construct("logout", "", $ssnid, $xfile);
$logoutres= do_query_and_get_doc($url, $logoutdoc);
if ($xfile !== false)
file_put_contents($xfile."_logout_response.xml", $logoutres);
// returns request and response document in array for the target function
return array($calldoc, $callres);
}
if (!isset($argv[4])) {
echo "Usage: ".$argv[0];
echo " <host> <username> <password> <function> <argstring> [export_name]";
echo "\n";
echo "php ".$argv[0];
echo " 10.47.48.1 admin kylone cpustat \"arg1=val1&arg2=val2\" cpustat_log";
echo "\n\n";
exit();
}
$v = do_apicall(
"http://".$argv[1]."/portal/", // URL
$argv[2], // Username
$argv[3], // Password
$argv[4], // Function name
(isset($argv[5]) ? $argv[5]: ""), // Parameters String
(isset($argv[6]) ? $argv[6]: false) // export each doucments to file
);
if (isset($argv[6])) {
echo "All requests and responses are exported to ".$argv[6]."_*.xml\n";
} else {
echo "Request:\n".$v[0]."\nResponse:\n".$v[1]."\n";
}
?>