| Server IP : 127.0.0.1 / Your IP : 192.168.99.1 Web Server : Apache/2.4.35 (Win64) OpenSSL/1.1.1b mod_fcgid/2.3.9 System : Windows NT WIN-JEJMB9EN0L0 10.0 build 14393 (Windows Server 2016) AMD64 User : Administrator ( 0) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /bpkd/admin/ |
Upload File : |
<?php
/**
* API Proxy untuk menghindari CORS dan masalah server-side
*/
// Aktifkan error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Set header JSON
header('Content-Type: application/json');
// Get POST data
$input = json_decode(file_get_contents('php://input'), true);
if (!$input) {
echo json_encode([
'error' => 'Invalid request',
'debug' => 'No input data received'
]);
exit;
}
$url = $input['url'] ?? '';
$method = $input['method'] ?? 'GET';
$timeout = (int)($input['timeout'] ?? 30);
$follow_redirects = $input['follow_redirects'] ?? true;
$verify_ssl = $input['verify_ssl'] ?? false; // Set default false untuk testing
$headers = $input['headers'] ?? [];
$body = $input['body'] ?? '';
// Validasi URL
if (!filter_var($url, FILTER_VALIDATE_URL)) {
echo json_encode([
'error' => 'Invalid URL',
'debug' => 'URL tidak valid: ' . $url
]);
exit;
}
// Inisialisasi cURL
$ch = curl_init();
// Set opsi cURL dasar
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $follow_redirects);
// Nonaktifkan SSL verification untuk testing
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// Set User-Agent
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
// Set encoding
curl_setopt($ch, CURLOPT_ENCODING, '');
// Set headers
if (!empty($headers)) {
$header_array = [];
foreach ($headers as $key => $value) {
$header_array[] = "$key: $value";
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
}
// Set body untuk POST/PUT
if (in_array($method, ['POST', 'PUT', 'PATCH']) && !empty($body)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
// Mulai timing
$start_time = microtime(true);
// Eksekusi
$response = curl_exec($ch);
$execution_time = microtime(true) - $start_time;
// Get info
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
// Debug info
$debug_info = [
'url' => $url,
'method' => $method,
'http_code' => $http_code,
'content_type' => $content_type,
'curl_error' => curl_error($ch),
'curl_errno' => curl_errno($ch)
];
if (curl_error($ch)) {
echo json_encode([
'error' => 'cURL Error: ' . curl_error($ch),
'debug' => $debug_info
]);
curl_close($ch);
exit;
}
// Pisahkan header dan body
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$response_headers = substr($response, 0, $header_size);
$response_body = substr($response, $header_size);
curl_close($ch);
// Cek apakah response adalah JSON
$is_json = false;
$json_data = null;
$body_formatted = $response_body;
if (strpos($content_type, 'application/json') !== false ||
(isset($response_body[0]) && ($response_body[0] === '{' || $response_body[0] === '['))) {
$json_data = json_decode($response_body, true);
if (json_last_error() === JSON_ERROR_NONE) {
$is_json = true;
$body_formatted = json_encode($json_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
} else {
$debug_info['json_error'] = json_last_error_msg();
}
}
// Kirim response
echo json_encode([
'status' => $http_code,
'status_message' => getHttpStatusMessage($http_code),
'headers' => $response_headers,
'body' => $response_body,
'body_formatted' => $body_formatted,
'raw' => $response,
'content_type' => $content_type,
'execution_time' => $execution_time,
'is_json' => $is_json,
'json_data' => $json_data,
'method' => $method,
'url' => $url,
'debug' => $debug_info
]);
function getHttpStatusMessage($code) {
$messages = [
200 => 'OK',
201 => 'Created',
400 => 'Bad Request',
401 => 'Unauthorized',
403 => 'Forbidden',
404 => 'Not Found',
500 => 'Internal Server Error'
];
return $messages[$code] ?? 'Unknown';
}
?>