forked from sharpspring/strading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Request.php
executable file
·189 lines (164 loc) · 5.67 KB
/
Request.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace Gajus\Strading;
/**
* @link https://github.com/gajus/strading for the canonical source repository
* @license https://github.com/gajus/strading/blob/master/LICENSE BSD 3-Clause
*/
class Request
{
private
/**
* @var string
*/
$interface_url,
/**
* @var array
*/
$headers = array('Content-Type: text/xml;charset=utf-8', 'Accept: text/xml'),
/**
* @var SimpleXMLElement
*/
$xml,
/**
* @var string
*/
$type;
/**
* @param string $interface_url
* @param string $site_reference
* @param string $username
* @param string $password
* @param SimpleXMLElement|\SimpleXMLElement $xml
* @throws Exception\InvalidArgumentException
*/
public function __construct($interface_url, $site_reference, $username, $password, \SimpleXMLElement $xml)
{
$this->interface_url = $interface_url;
$this->headers[] = 'Authorization: Basic ' . base64_encode($username . ':' . $password);
$this->xml = $xml;
// @todo What about multipart requests?
// PHP 5.3 does not allow array access to the method response.
$this->type = $this->xml->xpath('/requestblock/request');
$this->type = $this->type[0]->attributes();
$this->type = (string)$this->type['type'];
if ($this->getType() === 'TRANSACTIONQUERY') {
$this->populate(array(
'alias' => $username,
'request/filter/sitereference' => $site_reference
), '/requestblock');
} else if ($this->getType() === 'TRANSACTIONUPDATE') {
$this->populate(array(
'alias' => $username,
'request/filter/sitereference' => $site_reference
), '/requestblock');
} else {
$this->populate(array(
'alias' => $username,
'request/operation/sitereference' => $site_reference
), '/requestblock');
}
}
/**
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Populate XML template using data from an array.
*
* @param array $data ['node name' => 'text node value', 'another node[attribute]' => 'attribute value']
* @param string $namespace XML namespace under which the node resides, e.g. /requestblock/request
* @return null
* @throws Exception\InvalidArgumentException
*/
public function populate(array $data, $namespace = '')
{
foreach ($data as $k => $v) {
if (is_array($v)) {
$this->populate($v, $namespace . '/' . $k);
} else {
$attribute = null;
if (($attribute_position = strpos($k, '[')) !== false) {
$attribute = substr($k, $attribute_position + 1, -1);
$k = substr($k, 0, $attribute_position);
}
$element = $this->xml->xpath($namespace . '/' . $k);
if (count($element) === 0) {
throw new Exception\InvalidArgumentException($namespace . '/' . $k . ' path does not refer to an existing element.');
} else if (count($element) > 1) {
throw new \InvalidArgumentException($namespace . '/' . $k . ' path is referring to multiple elements.');
}
if ($attribute) {
$element[0]->addAttribute($attribute, $v);
} else {
dom_import_simplexml($element[0])->nodeValue = $v;
}
}
}
}
/**
* Request XML stripped of empty tags without attributes.
*
* @return string
*/
public function getXML()
{
$dom = new \DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($this->xml->asXML());
$xpath = new \DOMXPath($dom);
// Remove empty tags
// @see http://stackoverflow.com/a/21492078/368691
while (($node_list = $xpath->query('//*[not(*) and not(@*) and not(text()[normalize-space()])]')) && $node_list->length) {
foreach ($node_list as $node) {
$node->parentNode->removeChild($node);
}
}
// Remove text nodes that have only whitespace
$node_list = $xpath->query("//*[not(*) and not(normalize-space())]");
foreach ($node_list as $node) {
$node->nodeValue = '';
$node->removeChild($node->firstChild);
}
$xml = $dom->saveXML();
return $xml;
}
/**
* @return array
*/
public function getHeaders()
{
return $this->headers;
}
/**
* Issue the request.
* @return Gajus\Strading\Response
* @throws Exception\RuntimeException
*/
public function request()
{
$ch = curl_init();
$options = array(
CURLOPT_URL => $this->interface_url,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 10,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_HTTPHEADER => $this->getHeaders(),
CURLOPT_POSTFIELDS => $this->getXML()
);
curl_setopt_array($ch, $options);
$raw_response = curl_exec($ch);
$info = curl_getinfo($ch);
if ($info['http_code'] !== 200) {
throw new Exception\RuntimeException($raw_response);
}
$response = new \SimpleXMLElement($raw_response);
return new Response($response, $this);
}
}