Last active
April 2, 2018 10:22
-
-
Save trinvh/6feff507a0253861ba1a2c1bd23d1ac7 to your computer and use it in GitHub Desktop.
router.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('display_errors', 1); | |
ini_set('display_startup_errors', 1); | |
error_reporting(E_ALL); | |
$DOMAIN = 'api.mixcord.co'; | |
$site = "https://" . $DOMAIN; | |
$request = $_SERVER["REQUEST_URI"]; | |
$url = $site . $request; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); | |
if (in_array($_SERVER['REQUEST_METHOD'], ['POST', 'PATCH', 'PUT', 'DELETE'])) { | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $_SERVER['REQUEST_METHOD']); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('php://input')); | |
} | |
$headers = getallheaders(); | |
$headers["Host"] = $DOMAIN; | |
$strHeaders = []; | |
foreach ($headers as $key => $val) { | |
$strHeaders[] = $key . ':' . $val; | |
} | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $strHeaders); | |
curl_setopt($ch, CURLOPT_HEADER, true); | |
curl_setopt($ch, CURLINFO_HEADER_OUT, true); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$response = curl_exec($ch); | |
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); | |
$headers = substr($response, 0, $header_size); | |
$body = substr($response, $header_size); | |
curl_close($ch); | |
$headerArray = explode(PHP_EOL, $headers); | |
foreach ($headerArray as $header) { | |
header($header, false); | |
} | |
echo $body; | |
exit(); | |
function getallheaders() | |
{ | |
if (!is_array($_SERVER)) { | |
return array(); | |
} | |
$headers = array(); | |
foreach ($_SERVER as $name => $value) { | |
if (substr($name, 0, 5) == 'HTTP_') { | |
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; | |
} | |
} | |
return $headers; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment