1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
5
|
|
|
* To change this template file, choose Tools | Templates |
6
|
|
|
* and open the template in the editor. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Gendoria\CruftFlake\Config; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Description of ConsulCurlRequestor |
13
|
|
|
* |
14
|
|
|
* @author Tomasz Struczyński <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class ConsulCurl |
17
|
|
|
{ |
18
|
|
|
private $consulBaseUrl; |
19
|
|
|
|
20
|
|
|
private $apiPrefix = "/v1"; |
21
|
|
|
|
22
|
7 |
|
function __construct($consulBaseUrl, $apiPrefix = "/v1") |
|
|
|
|
23
|
|
|
{ |
24
|
7 |
|
$this->consulBaseUrl = $consulBaseUrl; |
25
|
7 |
|
$this->apiPrefix = $apiPrefix; |
26
|
7 |
|
} |
27
|
|
|
|
28
|
|
|
public function performGetRequest($url) |
29
|
|
|
{ |
30
|
|
|
$curlUrl = $this->consulBaseUrl . $this->apiPrefix . $url; |
31
|
|
|
$ch = curl_init($curlUrl); |
32
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
33
|
|
|
$result = curl_exec($ch); |
34
|
|
|
$returnData = json_decode($result, true); |
35
|
|
|
return $returnData; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function performPutRequest($url, $payload = null) |
39
|
|
|
{ |
40
|
|
|
$curlUrl = $this->consulBaseUrl . $this->apiPrefix . $url; |
41
|
|
|
$ch = curl_init($curlUrl); |
42
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); |
43
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); |
44
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 6); |
45
|
|
|
if ($payload !== null) { |
46
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); |
47
|
|
|
} |
48
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
49
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, |
50
|
|
|
array( |
51
|
|
|
'Content-Type: application/json', |
52
|
|
|
'Content-Length: ' . strlen($payload)) |
53
|
|
|
); |
54
|
|
|
$result = curl_exec($ch); |
55
|
|
|
$returnData = json_decode($result, true); |
56
|
|
|
return $returnData; |
57
|
|
|
} |
58
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.