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