-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathClient.php
More file actions
115 lines (100 loc) · 2.61 KB
/
Client.php
File metadata and controls
115 lines (100 loc) · 2.61 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
<?php
namespace QuickPay\API;
/**
* @class QuickPay_Client
* @since 0.1.0
* @package QuickPay
* @category Class
* @author Patrick Tolvstein, Perfect Solution ApS
* @docs http://tech.quickpay.net/api/
*/
class Client
{
/**
* Contains cURL instance
*
* @access public
*/
public $ch;
/**
* Base url for the selected API.
*
* @var string
*/
public $base_url;
/**
* Contains the authentication string
*
* @access protected
*/
protected $auth_string;
/**
* __construct function.
*
* Instantiate object
*
* @access public
* @param string $auth_string Format 'username:password' or ':apiKey'
* @param string $base_url The API to call. Use on of the constants.
* @throws Exception
*/
public function __construct($auth_string = '', $base_url = Constants::API_URL)
{
// Check if lib cURL is enabled
if (!function_exists('curl_init')) {
throw new Exception('Lib cURL must be enabled on the server');
}
// Set auth string property
$this->auth_string = $auth_string;
// Set base url of selected API
$this->base_url = $base_url;
// Instantiate cURL object
$this->authenticate();
}
/**
* Shutdown function.
*
* Closes the current cURL connection
*
* @access public
*/
public function shutdown()
{
if (!empty($this->ch)) {
curl_close($this->ch);
}
}
/**
* authenticate function.
*
* Create a cURL instance with authentication headers
*
* @access public
*/
protected function authenticate()
{
$this->ch = curl_init();
$headers = array();
switch ($this->base_url) {
case Constants::API_URL_INVOICING:
$headers[] = 'Accept: application/vnd.api+json';
break;
case Constants::API_URL:
$headers[] = 'Accept-Version: v' . Constants::API_VERSION;
$headers[] = 'Accept: application/json';
break;
default:
break;
}
if (!empty($this->auth_string)) {
$headers[] = 'Authorization: Basic ' . base64_encode($this->auth_string);
}
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_HTTPHEADER => $headers
);
curl_setopt_array($this->ch, $options);
}
}