-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI-Forwarder.php
91 lines (51 loc) · 1.51 KB
/
API-Forwarder.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
<?php
// PHP API Forwarder
// Copyright (c)2022 - RND ICWR - Afrizal F.A
namespace APIForwarder;
class API
{
public function __construct()
{
$this->domain = '';
$this->ssl = '';
}
public function requester($url, $method, $headers, $body = null)
{
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
if (!empty($body)) {
curl_setopt($c, CURLOPT_POSTFIELDS, $body);
}
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($c);
curl_close($c);
return $response;
}
public function forwarder()
{
if (!empty($this->ssl) && $this->ssl == true) {
$protocol = "https://";
} else {
$protocol = "http://";
}
$requestMethod = $_SERVER['REQUEST_METHOD'];
if (!empty($_GET['path'])) {
$url = $protocol . $this->domain . "/" . $_GET['path'];
} else {
$url = $protocol . $this->domain;
}
$headers = [];
foreach(getallheaders() as $key => $value)
{
$headers []= $key . ": " . $value;
}
if (!empty(file_get_contents("php://input"))) {
$body = file_get_contents("php://input");
} else {
$body = null;
}
return $this->requester($url, $requestMethod, $headers, $body);
}
}