php class UniversalFakeAPI public static function run uri logs section

 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
<?php
class UniversalFakeAPI {
public static function run($uri, $logs = [], $sections = []) {
$post_data = file_get_contents('php://input');
$request = count($sections) != 0 ? implode('/', $sections) : '';
$result_uri = $uri . '/' . (!empty($request) ? $request : '') . '/' . (!empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '');
$ch = curl_init();
if(preg_match('/^https/', $uri)) {
curl_setopt_array($ch, array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
));
}
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $result_uri
));
if(!empty(trim($post_data))) {
curl_setopt_array($ch, array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $post_data
));
}
$res = curl_exec($ch);
echo $res;
if(isset($logs['client_log'])) {
$fp = fopen('logs/client/' . $logs['client_log'], 'a+');
fwrite($fp, 'IP: ' . $_SERVER['REMOTE_ADDR'] . '; Date: ' . date('Y-m-d H:i:s') . '; URI: ' . $result_uri . "\nPOST: \n----------\n" . $post_data . "\n----------\n\n");
fclose($fp);
}
if(isset($logs['server_log'])) {
$fp = fopen('logs/server/' . $logs['server_log'], 'a+');
fwrite($fp, 'IP: ' . $_SERVER['REMOTE_ADDR'] . '; Date: ' . date('Y-m-d H:i:s') . '; URI: ' . $result_uri . "\nPOST: \n----------\n" . $post_data . "\n----------\nSERVER return: \n----------\n" . $res . "\n-----------\n\n");
fclose($fp);
}
curl_close($ch);
}
}