-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpRequest.php
155 lines (134 loc) · 3.98 KB
/
HttpRequest.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace OxidEsales\MonduPayment\Core\Http;
use OxidEsales\MonduPayment\Core\Exception\InvalidRequestException;
class HttpRequest
{
/**
* curl instance
*
* @var [curl]
*/
private $curl;
/**
* headers of the request
*
* @var array
*/
private array $headers;
/**
* baseUrl of the request
*
* @var array
*/
private string $baseUrl;
public function __construct(string $baseUrl, array $headers = ['Content-type' => 'application/json'])
{
$this->curl = curl_init();
$this->baseUrl = $baseUrl;
$this->headers = $headers;
}
/**
* get Request
*
* @param string $url
* @param array $data
* @param array $headers
* @return void
*/
public function get(string $url, array $data = [], array $headers = null)
{
$url = $this->baseUrl . $url;
$this->headers = $headers == null ? $this->headers : $headers;
return $this->send_request($url, $data, 'GET');
}
/**
* POST Request
*
* @param string $url
* @param array $data
* @param array $headers
* @return void
*/
public function post(string $url, array $data = [], array $headers = null)
{
$url = $this->baseUrl . $url;
$this->headers = $headers == null ? $this->headers : $headers;
return $this->send_request($url, $data, 'POST');
}
/**
* PATCH Request
*
* @param string $url
* @param array $data
* @param array $headers
* @return void
*/
public function patch(string $url, array $data = [], array $headers = ['Content-type' => 'application/json'])
{
$url = $this->baseUrl . $url;
$this->headers = $headers;
return $this->send_request($url, $data, 'PATCH');
}
/**
* PUT Request
*
* @param string $url
* @param array $data
* @param array $headers
* @return void
*/
public function put(string $url, array $data = [], array $headers = ['Content-type' => 'application/json'])
{
$url = $this->baseUrl . $url;
$this->headers = $headers;
return $this->send_request($url, $data, 'PUT');
}
/**
* DELETE Request
*
* @param string $url
* @param array $data
* @param array $headers
* @return void
*/
public function delete(string $url, array $data = [], array $headers = ['Content-type' => 'application/json'])
{
$url = $this->baseUrl . $url;
$this->headers = $headers;
return $this->send_request($url, $data, 'DELETE');
}
/**
* for sending request
*
* @param array $data
* @param boolean $token
* @return array $response
*/
public function send_request(string $url, array $data, string $method)
{
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->headers);
if ($method === 'POST') {
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($data));
}
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($this->curl, CURLOPT_TIMEOUT, 10);
$response = curl_exec($this->curl);
$httpCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
curl_close($this->curl);
if (!$response && $httpCode > 308) {
throw new InvalidRequestException('[MONDU__ERROR] Request can not be processed.', $data);
}
$response = json_decode($response, true);
if (
(isset($response['errors']) && $response['errors'] != null) ||
(isset($response['error']) && $response['error'] != null)
) {
throw new InvalidRequestException('[MONDU__ERROR] ' . json_encode($response), $data, $response);
}
return $response;
}
}