-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_procedure.php
73 lines (60 loc) · 2.04 KB
/
http_procedure.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
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
<?php
require_once('http.php');
class http_procedure extends procedure
{
function __construct($datasource, $vars, $name, $method, $url, $params, $get, $post, $content_type, $empty, $root, $permission = null, $cache = true)
{
parent::__construct($vars, $name, $params, $empty, $root, [], $permission, $cache);
$this->datasource = $datasource;
$this->method = $method;
$this->url = $datasource['url'] . $url;
$this->get = $get;
$this->post = $post;
$this->content_type = is_null($content_type) ? $datasource['content-type'] : $content_type;
}
function query($args, $document)
{
$this->validate($args);
$xml = new xml();
$get = [];
foreach($this->get as $name => $value)
{
$get[$name] = vars::apply_assoc($value, $args);
}
switch($this->method)
{
case 'get':
return self::to_xml(http::get($this->url, $get, $this->datasource['username'], $this->datasource['password']));
case 'post':
{
$post = [];
foreach($this->post as $name => $value)
{
$post[$name] = vars::apply_assoc($value, $args);
}
return self::to_xml(http::post($this->url, $post, $get, $this->datasource['username'], $this->datasource['password']));
}
default:
runtime_error('Unknown HTTP procedure method: ' . $this->method);
}
}
private function to_xml($result)
{
switch($this->content_type)
{
case 'xml':
return xml::parse($result);
case 'json':
return xml::json($this->root[0], $result);
default:
runtime_error('Unknown HTTP procedure content type: ' . $this->content_type);
}
}
private $datasource;
private $method;
private $url;
private $get;
private $post;
private $content_type;
}
?>