forked from gamefamorg/examapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiCaller.php
83 lines (70 loc) · 2.67 KB
/
apiCaller.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
<?php
// apiCaller.php
class ApiCaller {
private $baseUrl;
private $bearerToken;
public function __construct($baseUrl, $bearerToken = null) {
$this->baseUrl = rtrim($baseUrl, '/');
$this->bearerToken = $bearerToken;
}
public function call($method, $endpoint, $data = null) {
$url = $this->baseUrl . '/' . ltrim($endpoint, '/');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // Timeout sau 10 giây nếu không kết nối được
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // Timeout tổng cộng sau 30 giây
$headers = [
'Content-Type: application/json',
'Accept: application/json'
];
if ($this->bearerToken) {
$headers[] = "Authorization: Bearer {$this->bearerToken}";
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
switch (strtoupper($method)) {
case 'GET':
if ($data) {
$url .= '?' . http_build_query($data);
curl_setopt($ch, CURLOPT_URL, $url);
}
break;
case 'POST':
curl_setopt($ch, CURLOPT_POST, true);
if ($data) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
break;
case 'PUT':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
if ($data) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
break;
case 'DELETE':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
if ($data) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
break;
default:
break; //throw new Exception("Unsupported HTTP method: $method");
}
$response = curl_exec($ch);
$error = curl_error($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($error) {
return ['status' => 500, 'body' => null];
//throw new Exception("cURL Error: $error. Status code: $statusCode");
}
$decodedResponse = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return ['status' => 500, 'body' => null];//throw new Exception("Invalid JSON response: " . json_last_error_msg());
}
return [
'status' => $statusCode,
'body' => $decodedResponse
];
}
}