Skip to content

Instantly share code, notes, and snippets.

@pinfort
Last active November 7, 2018 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinfort/7bdcb0047a2ade84e591ddb5b1126f9a to your computer and use it in GitHub Desktop.
Save pinfort/7bdcb0047a2ade84e591ddb5b1126f9a to your computer and use it in GitHub Desktop.
<?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