Last active
November 7, 2018 03:07
-
-
Save pinfort/7bdcb0047a2ade84e591ddb5b1126f9a to your computer and use it in GitHub Desktop.
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 | |
namespace zonuexe\ZoProxy; | |
// サブドメインで公開する場合、このパスが一階層変わります。 | |
require_once(dirname(__FILE__).'/../../railsinstaller/php/vendor/autoload.php'); | |
use GuzzleHttp\Client as HttpClient; | |
use GuzzleHttp\Psr7; | |
// ここはいい感じにやってね | |
$host_table = [ | |
'localhost' => [ | |
'host' => 'localhost', | |
'port' => railsのぽーと, | |
'scheme' => 'http', | |
], | |
'おもてのドメイン名' => [ | |
'host' => 'localhost', | |
'port' => railsのぽーと, | |
'scheme' => 'http', | |
] | |
]; | |
$request = Psr7\ServerRequest::fromGlobals(); | |
$new_uri = $request->getUri()->withPort(80); | |
$key = $new_uri->getHost(); | |
$port = $new_uri->getPort(); | |
//if ($port !== null && !Psr7\Uri::isDefaultPort($new_uri)) { | |
// $key .= ":{$port}"; | |
//} | |
// コメントアウトしないとSSLがうまくいかなかった。 | |
if (isset($host_table[$key])) { | |
if (isset($host_table[$key]['host'])) { | |
$new_uri = $new_uri->withHost($host_table[$key]['host']); | |
} | |
if (isset($host_table[$key]['port'])) { | |
$new_uri = $new_uri->withPort($host_table[$key]['port']); | |
} | |
if (isset($host_table[$key]['scheme'])) { | |
$new_uri = $new_uri->withScheme($host_table[$key]['scheme']); | |
} | |
} | |
$client = new HttpClient; | |
$response = $client->send($request->withUri($new_uri), [ | |
'http_errors' => false, | |
]); | |
foreach ($response->getHeaders() as $key => $values) { | |
if($key !== 'Transfer-Encoding'){ | |
// Transfer-Encodingを除外しないと内部リダイレクトループになった。なんでだろう? | |
foreach ($values as $value) { | |
header("{$key}:{$value}"); | |
} | |
} | |
} | |
echo $response->getBody(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment