forked from doubotis/php-rest-wrapper
/
api_v1.php
47 lines (32 loc) · 972 Bytes
/
api_v1.php
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
<?php
include_once 'utils/xml-converter.php';
include_once 'utils/strings.php';
include_once 'classes/api-request.php';
include_once 'classes/api-response.php';
include_once 'classes/api-response-handler.php';
$timestampStart = time();
$requestURI = $_SERVER['REQUEST_URI'];
$request = new APIRequest($requestURI);
$handler = new APIResponseHandler($request);
$obj = $handler->getObject();
$response = new APIResponse();
$response->setData($obj);
$timestampEnd = time();
$response->setDelay($timestampEnd - $timestampStart);
// Prepares the result
$arr = array(
"request" => $request->toArray(),
"response" => $response->toArray()
);
if ($request->getExtension() == "json") {
$json = json_encode($arr);
header('Content-Type: text/json');
echo $json;
}
else if ($request->getExtension() == "xml") {
$xml = new SimpleXMLElement('<root/>');
array_to_xml($arr,$xml);
header('Content-Type: text/xml');
echo $xml->asXML();
}
?>