-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathHTTP.php
More file actions
executable file
·224 lines (205 loc) · 6.99 KB
/
HTTP.php
File metadata and controls
executable file
·224 lines (205 loc) · 6.99 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php // vim:set ts=4 sw=4 et:
namespace ElasticSearch\Transport;
/**
* This file is part of the ElasticSearch PHP client
*
* (c) Raymond Julin <raymond.julin@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
if (!defined('CURLE_OPERATION_TIMEDOUT'))
define('CURLE_OPERATION_TIMEDOUT', 28);
class HTTP extends Base {
/**
* How long before timing out CURL call
*/
const TIMEOUT = 5;
/**
* curl handler which is needed for reusing existing http connection to the server
* @var resource
*/
protected $ch;
public function __construct($host='localhost', $port=9200) {
parent::__construct($host, $port);
$this->ch = curl_init();
}
/**
* Index a new document or update it if existing
*
* @return array
* @param array $document
* @param mixed $id Optional
* @param array $options
*/
public function index($document, $id=false, array $options = array()) {
$url = $this->buildUrl(array($this->type, $id), $options);
$method = ($id == false) ? "POST" : "PUT";
return $this->call($url, $method, $document);
}
/**
* Search
*
* @return array
* @param array|string $query
* @param array $options
*/
public function search($query, array $options = array()) {
if (is_array($query)) {
/**
* Array implies using the JSON query DSL
*/
$url = $this->buildUrl(array(
$this->type, "_search"
));
$result = $this->call($url, "GET", $query);
}
elseif (is_string($query)) {
/**
* String based search means http query string search
*/
$url = $this->buildUrl(array(
$this->type, "_search?q=" . $query
));
$result = $this->call($url, "POST", $options);
}
return $result;
}
/**
* Search
*
* @return array
* @param mixed $query
* @param array $options Parameters to pass to delete action
*/
public function deleteByQuery($query, array $options = array()) {
$options += array(
'refresh' => true
);
if (is_array($query)) {
/**
* Array implies using the JSON query DSL
*/
$url = $this->buildUrl(array($this->type, "_query"));
$result = $this->call($url, "DELETE", $query);
}
elseif (is_string($query)) {
/**
* String based search means http query string search
*/
$url = $this->buildUrl(array($this->type, "_query"), array('q' => $query));
$result = $this->call($url, "DELETE");
}
if ($options['refresh']) {
$this->request('_refresh', "POST");
}
return !isset($result['error']) && $result['ok'];
}
/**
* Perform a request against the given path/method/payload combination
* Example:
* $es->request('/_status');
*
* @param string|array $path
* @param string $method
* @param array|bool $payload
* @return array
*/
public function request($path, $method="GET", $payload=false) {
return $this->call($this->buildUrl($path), $method, $payload);
}
/**
* Flush this index/type combination
*
* @return array
* @param mixed $id Id of document to delete
* @param array $options Parameters to pass to delete action
*/
public function delete($id=false, array $options = array()) {
if ($id)
return $this->request(array($this->type, $id), "DELETE");
else
return $this->request(false, "DELETE");
}
/**
* Perform a http call against an url with an optional payload
*
* @return array
* @param string $url
* @param string $method (GET/POST/PUT/DELETE)
* @param array|bool $payload The document/instructions to pass along
* @throws HTTPException
*/
protected function call($url, $method="GET", $payload=false) {
$conn = $this->ch;
$protocol = "http";
$requestURL = $protocol . "://" . $this->host . $url;
curl_setopt($conn, CURLOPT_URL, $requestURL);
curl_setopt($conn, CURLOPT_TIMEOUT, self::TIMEOUT);
curl_setopt($conn, CURLOPT_PORT, $this->port);
curl_setopt($conn, CURLOPT_RETURNTRANSFER, 1) ;
curl_setopt($conn, CURLOPT_CUSTOMREQUEST, strtoupper($method));
curl_setopt($conn, CURLOPT_FORBID_REUSE , 0) ;
if (is_array($payload) && count($payload) > 0)
curl_setopt($conn, CURLOPT_POSTFIELDS, json_encode($payload)) ;
else
curl_setopt($conn, CURLOPT_POSTFIELDS, null);
$response = curl_exec($conn);
if ($response !== false) {
$data = json_decode($response, true);
if (!$data) {
$data = array('error' => $response, "code" => curl_getinfo($conn, CURLINFO_HTTP_CODE));
}
}
else {
/**
* cUrl error code reference can be found here:
* http://curl.haxx.se/libcurl/c/libcurl-errors.html
*/
$errno = curl_errno($conn);
switch ($errno)
{
case CURLE_UNSUPPORTED_PROTOCOL:
$error = "Unsupported protocol [$protocol]";
break;
case CURLE_FAILED_INIT:
$error = "Internal cUrl error?";
break;
case CURLE_URL_MALFORMAT:
$error = "Malformed URL [$requestURL] -d " . json_encode($payload);
break;
case CURLE_COULDNT_RESOLVE_PROXY:
$error = "Couldnt resolve proxy";
break;
case CURLE_COULDNT_RESOLVE_HOST:
$error = "Couldnt resolve host";
break;
case CURLE_COULDNT_CONNECT:
$error = "Couldnt connect to host [{$this->host}], ElasticSearch down?";
break;
case CURLE_OPERATION_TIMEDOUT:
$error = "Operation timed out on [$requestURL]";
break;
default:
$error = "Unknown error";
if ($errno == 0)
$error .= ". Non-cUrl error";
break;
}
$exception = new HTTPException($error);
$exception->payload = $payload;
$exception->port = $this->port;
$exception->protocol = $protocol;
$exception->host = $this->host;
$exception->method = $method;
throw $exception;
}
// callback
if ($this->onCall) {
foreach ($this->onCall as $cb) {
call_user_func($cb, $url, $method, $payload, $data);
}
}
return $data;
}
}