1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lenius\Economic\API; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @class Economic_Client |
7
|
|
|
*/ |
8
|
|
|
class Client |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Contains cURL instance. |
12
|
|
|
*/ |
13
|
|
|
public $ch; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Contains the authentication string. |
17
|
|
|
*/ |
18
|
|
|
protected $secret_token; |
19
|
|
|
|
20
|
|
|
protected $grant_token; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* __construct function. |
24
|
|
|
* |
25
|
|
|
* Instantiate object |
26
|
|
|
* |
27
|
|
|
* @param string $secret_token |
28
|
|
|
* @param string $grant_token |
29
|
|
|
* |
30
|
|
|
* @throws Exception |
31
|
|
|
*/ |
32
|
4 |
|
public function __construct($secret_token = '', $grant_token = '') |
33
|
|
|
{ |
34
|
|
|
// Check if lib cURL is enabled |
35
|
4 |
|
if (!function_exists('curl_init')) { |
36
|
|
|
throw new Exception('Lib cURL must be enabled on the server'); |
37
|
|
|
} |
38
|
|
|
|
39
|
4 |
|
if (empty($secret_token)) { |
40
|
|
|
throw new Exception('secret token is missing'); |
41
|
|
|
} |
42
|
|
|
|
43
|
4 |
|
if (empty($grant_token)) { |
44
|
|
|
throw new Exception('grant token is missing'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Set auth string property |
48
|
4 |
|
$this->secret_token = $secret_token; |
49
|
4 |
|
$this->grant_token = $grant_token; |
50
|
|
|
} |
51
|
|
|
|
52
|
4 |
|
/** |
53
|
4 |
|
* Shutdown function. |
54
|
|
|
* |
55
|
|
|
* Closes the current cURL connection |
56
|
|
|
*/ |
57
|
|
|
public function shutdown() |
58
|
|
|
{ |
59
|
|
|
if (!empty($this->ch)) { |
60
|
|
|
curl_close($this->ch); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create function. |
66
|
|
|
* |
67
|
|
|
* Create cURL connection with authentication |
68
|
|
|
*/ |
69
|
|
|
public function create() |
70
|
|
|
{ |
71
|
|
|
if (!empty($this->ch)) { |
72
|
4 |
|
curl_close($this->ch); |
73
|
|
|
} |
74
|
4 |
|
|
75
|
|
|
// Instantiate cURL object |
76
|
|
|
$this->ch = curl_init(); |
77
|
4 |
|
|
78
|
4 |
|
// Apply authentication headers |
79
|
4 |
|
$this->authenticate(); |
80
|
|
|
} |
81
|
4 |
|
|
82
|
4 |
|
/** |
83
|
4 |
|
* authenticate function. |
84
|
|
|
* |
85
|
4 |
|
* Create authentication headers |
86
|
4 |
|
*/ |
87
|
4 |
|
protected function authenticate() |
88
|
|
|
{ |
89
|
|
|
$headers = [ |
90
|
|
|
'Accept: application/json', |
91
|
4 |
|
'Content-Type: application/json; charset=utf-8', |
92
|
4 |
|
]; |
93
|
4 |
|
|
94
|
4 |
|
if (!empty($this->secret_token)) { |
95
|
4 |
|
$headers[] = 'X-AppSecretToken:'.$this->secret_token; |
96
|
|
|
} |
97
|
4 |
|
|
98
|
4 |
|
if (!empty($this->grant_token)) { |
99
|
|
|
$headers[] = 'X-AgreementGrantToken:'.$this->grant_token; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
//default headers |
103
|
|
|
$options = [ |
104
|
|
|
CURLOPT_RETURNTRANSFER => true, |
105
|
|
|
CURLOPT_SSL_VERIFYPEER => true, |
106
|
|
|
CURLOPT_HTTPAUTH => CURLAUTH_BASIC, |
107
|
|
|
CURLOPT_HTTPHEADER => $headers, |
108
|
|
|
]; |
109
|
|
|
|
110
|
|
|
curl_setopt_array($this->ch, $options); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|