-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobotRestClient.class.php
182 lines (166 loc) · 4.81 KB
/
RobotRestClient.class.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
<?php
/**
* Basic REST client
*
* Copyright (c) 2013-2016 Hetzner Online GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
class RobotRestClient
{
private $curl;
private $curlOptions = array();
protected $httpHeader = array();
protected $baseUrl;
/**
* Class constructor
*
* @param $url Robot webservice url
* @param $login Robot login name
* @param $password Robot password
* @param $verbose
*/
public function __construct($url, $login, $password, $verbose = false)
{
$this->baseUrl = rtrim($url, '/');
$this->curl = curl_init();
$this->setCurlOption(CURLOPT_RETURNTRANSFER, true);
$this->setCurlOption(CURLOPT_USERPWD, $login . ':' . $password);
$this->setCurlOption(CURLOPT_VERBOSE, $verbose);
}
/**
* Class destructor
*/
public function __destruct()
{
curl_close($this->curl);
}
/**
* Set a curl option
*
* @param $option CURLOPT option constant
* @param $value
*/
protected function setCurlOption($option, $value)
{
$this->curlOptions[$option] = $value;
}
/**
* Get value for a curl option
*
* @param $option CURLOPT option constant
* @return mixed The value
*/
protected function getCurlOption($option)
{
return isset($this->curlOptions[$option]) ? $this->curlOptions[$option] : null;
}
/**
* Set a HTTP header
*
* @param $name
* @param $value
*/
public function setHttpHeader($name, $value)
{
$this->httpHeader[$name] = $name . ': ' . $value;
}
/**
* Do a GET request
*
* @param $url
* @return array Array with keys 'response_code' and 'response'
* On error 'response' is false
*/
protected function get($url)
{
$this->setCurlOption(CURLOPT_URL, $url);
$this->setCurlOption(CURLOPT_HTTPGET, true);
$this->setCurlOption(CURLOPT_CUSTOMREQUEST, 'GET');
return $this->executeRequest();
}
/**
* Do a POST request
*
* @param $url
* @param $data Post data
* @return array Array with keys 'response_code' and 'response'
* On error 'response' is false
*/
protected function post($url, array $data = array())
{
$this->setCurlOption(CURLOPT_URL, $url);
$this->setCurlOption(CURLOPT_POST, true);
$this->setCurlOption(CURLOPT_CUSTOMREQUEST, 'POST');
if ($data)
{
$this->setCurlOption(CURLOPT_POSTFIELDS, http_build_query($data));
}
return $this->executeRequest();
}
/**
* Do a PUT request
*
* @param $url
* @param $data Put data
* @return array Array with keys 'response_code' and 'response'
* On error 'response' is false
*/
protected function put($url, array $data = array())
{
$this->setCurlOption(CURLOPT_URL, $url);
$this->setCurlOption(CURLOPT_HTTPGET, true);
$this->setCurlOption(CURLOPT_CUSTOMREQUEST, 'PUT');
if ($data)
{
$this->setCurlOption(CURLOPT_POSTFIELDS, http_build_query($data));
}
return $this->executeRequest();
}
/**
* Do a DELETE request
*
* @param $url
* @return array Array with keys 'response_code' and 'response'
* On error 'response' is false
*/
protected function delete($url)
{
$this->setCurlOption(CURLOPT_URL, $url);
$this->setCurlOption(CURLOPT_HTTPGET, true);
$this->setCurlOption(CURLOPT_CUSTOMREQUEST, 'DELETE');
return $this->executeRequest();
}
/**
* Execute HTTP request
*
* @return array Array with keys 'response_code' and 'response'
* On error 'response' is false
*/
protected function executeRequest()
{
$this->setCurlOption(CURLOPT_HTTPHEADER, array_values($this->httpHeader));
curl_setopt_array($this->curl, $this->curlOptions);
$response = curl_exec($this->curl);
return array(
'response_code' => curl_getinfo($this->curl, CURLINFO_HTTP_CODE),
'response' => $response
);
}
}