-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathelastic.php
91 lines (70 loc) · 2.27 KB
/
elastic.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
<?php
require_once (dirname(__FILE__) . '/config.inc.php');
$elastic = new ElasticSearch($config['elastic_options']);
//--------------------------------------------------------------------------------------------------
class ElasticSearch
{
//----------------------------------------------------------------------------------------------
function __construct($options)
{
foreach($options AS $key => $value) {
$this->$key = $value;
}
}
//----------------------------------------------------------------------------------------------
function send($method, $action_url = '', $post_data = NULL)
{
$ch = curl_init();
$url = $this->protocol . '://' . $this->host . ':' . $this->port . '/' . $this->index;
if ($action_url != '')
{
$url .= '/' . $action_url;
}
//echo $url . "\n";
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
if (isset($this->user))
{
curl_setopt($ch, CURLOPT_USERPWD, $this->user . ":" . $this->password);
}
// Set HTTP headers
$headers = array();
$headers[] = 'Content-type: application/json'; // we are sending JSON
// Override Expect: 100-continue header (may cause problems with HTTP proxies
// http://the-stickman.com/web-development/php-and-curl-disabling-100-continue-header/
$headers[] = 'Expect:';
curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
if (isset($this->proxy))
{
curl_setopt($ch, CURLOPT_PROXY, $this->proxy);
}
switch ($method) {
case 'POST':
curl_setopt($ch, CURLOPT_POST, TRUE);
if (!empty($post_data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
break;
case 'PUT':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
if (!empty($post_data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
break;
case 'DELETE':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
}
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//echo $response;
if (curl_errno ($ch) != 0 )
{
echo "CURL error: ", curl_errno ($ch), " ", curl_error($ch);
}
//echo $http_code . "\n";
return $response;
}
}
?>